So finally I am back. Where I was is a story for another day. What is important, however, is that I have a tasty new write-up for you.
Rather than try and fill in the gap from where I left off, I have decided to jump into something a little more advanced and conceptual. Keep in mind that to get anything out of this, you will need to have a basic grasp of what classes and objects are.
Inheritance and polymorphism are two of those ideas that I found a little confusing at first. With polymorphism especially, I found myself scratching my head trying to figure out how it would be useful. It is actually one of the cornerstones of object oriented design, and incredibly handy once you get your head around it.
Ok, so what are these things? I will start off with inheritance. Imagine that you are a car manufacturer, and you want to create a number of classes that represent your various models of cars. You have in your lineup a hatchback, a sedan and a 4×4. You could simply code all of them separately and write code for tooting the horn, have instance variables for the fuel level etc., but that would be replicating heaps of code. What if you wanted to change the speed at which the windscreen wipers go across your entire range? You would have to re-write, and debug three different versions of the same code. Sure, you could just copy and paste, but that is just as error prone, and counter productive.
What you really want is to write the windscreen wiper code once, and maintain just one copy of it. What mechanism can you use to do this? You guessed it: Inheritance. Collect your prize at the box office.
Inheritance allows you to have a Base Class with all common code in it that other classes can Derive From. Continuing with the car analogy, you would put your windscreen wiper code, along with your fuel level code and your headlight code along with any other code common amongst your cars in the base class. From there you would have derived classes representing your hatchback, sedan and 4×4.
Inheritance also has an important concept of overriding. This is where you have functionality in your base class that you want to replace in the derived class. In the case of your car, your drive chain code would be common between your hatchback and your sedan, but the 4×4 will be different. Rather than copy and paste between the sedan and the hatchback, you would put that functionality in the base class and override it in your 4×4 class.
So, lets draw a basic class hierarchy:
- Car (Base Class)
- Sedan
- HatchBack
- 4×4
The Car class might look a little something like this:
- Car
- useWipers (method)
- toggleHeadlights (method)
- getFuelLevel (method)
- changeGear (method)
- odometer (variable)
The 4×4, which has a different gearbox, might look like this:
- 4×4 inherits from Car
- use4WheelDrive (method)
- use2WheelDrive (method)
- changeGears (method, overridden)
The 4×4 will still have the useWipers method from the Car class, along with any other methods declared there, but it rewrites the changeGears method to suit itself. Also, it add functionality that is unique to itself, the use2WheelDrive and the use4WheelDrive methods.
This is an incredibly efficient way of writing code without duplication and redundancy. But what about the code supporting such things? Code that interacts would have to be able to deal with three different classes as though they were totally unrelated.
Or would it? Of course not, and this is where polymorphism comes into play.
Polymorphism sounds incredibly complicated, or like what happens when a parrot turns into a Power Ranger. It actually isn’t. All it really is is a way for you to classes derived from a base class as that base class. If we have some code that calls changeGears, we don’t want to write it for every different type of car. What we can do is instantiate a 4×4 class and place that object into a variable that is of the Car type, and we can then use all methods in the base class. what happens when you have overridden a method? Well, you get the overridden method!
One minor detail which I was trying to avoid was the concept of virtual methods. I’m still going to avoid it, but you can look it up on wikipedia for a nice definition. I don’t want to get into it because it is closely related to the language in which you are coding. In Java for example, you don’t even have to think about it, but in C++ you must explicitly deal with it in your code.
Anyway, I hope that this was of some use to somebody.