Inheritance & Polymorphism (& smelly butts)
I'm trying with these articles to provide simplified, visual representations of basic programming concepts. Inheritance and polymorphism are key elements of OOP (Object Oriented Programming), but they can be hard to visualize. I use Java terminology and conventions here, though the concepts are very similar in C# or other OOP languages.
Let's start with an abstract class called LivingThing. Because it is abstract, we can never really have something that is just a LivingThing, but we can deal with all living things at the same time. They are born in some way and die in some way. So, here are a bunch of living things which we might deal with as an array, LivingThing[], for example.
Groovy, but we really don't deal with apple trees the same way we deal with lions and antelopes and platypuses (oh my). So, let's create a "real" class, one we can instantiate. We'll call it Animal, and we say it extends LivingThing, meaning it has all the stuff from LivingThing, but its own extra rules and features and behaviors. Notice that the apple tree isn't here. It will have to be instantiated through some other class, like Plant or NonAnimal or whatever. Not my problem.
That is inheritance, and we talk about child classes and parent classes and so forth. It usually takes a more general case and makes a more specific case, which can go on and on. A Lion is-a Animal is-a LivingThing. Inheritance is what makes sure each level extends the more general case to a more specific case. The fact that we can treat a Lion as a Lion, or as an Animal, or as a LivingThing is polymorphism, a fancy word for treating one thing as multiple things.
Still, that isn't all there is to polymorphism. We can also have stuff that is true in different classes, even if they don't share a parent or grandparent. Maybe more like an uncle or second cousin, not close but they still get to show up at family reunions because they are related to you. For example, we could define an interface called Walkable. Walkable is said to be implemented by Lion or Platypus or Antelope because they can be walked. When we think about them as Walkable, we don't care much about anything but legs and what they do with them. They still eat and poop and make noise, but we don't worry about that stuff.
Recommended by LinkedIn
Looks pretty weird, right, but that is what an interface is like. It doesn't care whether these things eat or poop or have very sharp teeth, just about their legs. But we can still have a List<Walkable> which is a list of different walkable things of whatever their type. The Lion class is said to implement the Walkable interface, which means a Lion can be walked, though you might want a long leash.
Let's think of an interface that doesn't apply just to animals. How about the YummyToSomebody interface? Like Walkable, this only deals with one aspect of the different objects, the edible bits. (Hmm, if I added a Zombie class which implemented YummyToSomebody, I might have to include heads in my image next time.) But notice, it also includes the apples from the AppleTree. With polymorphism, we can work with a Map<YummyToSomebody>, perhaps keyed off which item they are on the menu.
We're almost done, I promise. But for the sake of completeness, I will leave you with one more interface that goes even beyond the LivingThing class, the SmellyWhenGassy interface. Many objects implement this, though some through their butts and others through their smokestacks. We don't care about that so long as they emit gas and it stinks. Because of that one thing they have in common, we could have a Queue<SmellyWhenGassy> and line them all up, but really, who would want to?
So, I hope that gives you a somewhat more visual way of thinking about inheritance and polymorphism. What should I talk about next?