Monday, 3 October 2011

Java: Inheritance

Java has a few possible different relationships between classes, possibly the 2 most important types being packages and inheritance. We`ve already seen packages, where several separate classes in the same package are essentially connected, such as in our Bridge Hand program. The latter, inheritance, is essentially a way to make a class an extension of another. This may not sound like much, but its a pretty key part of object oriented programming.

Inheritance, as the name suggests, allows a class (the subclass, the child) to take on, or inherit, all of the features of another class (the superclass, the parent), often demonstrated with the following simple diagram:

Don`t know why the arrow is pointing up, it seems like it would make more sense the other way around. But hey, doesn`t stop how the whole inheritance thing actually works. The subclass inherits all the variables and methods from the superclass, excepting the constructor. To make this something useful, the subclass can also  declare its own variables and methods, as well as redefining some superclass methods, normally known as overriding. This allows the subclass to specialize to its own needs, while holding the same general structure of the superclass.

Let`s have a couple of examples so its easier to understand. Say we have a very very general class: Vehicle. Then we can have several subclasses to fit the role of vehicle, but be specialized depending on what we want. We can have a Truck, Motorbike or a Car defined as a subclass of vehicle. We can say that our Car is a Vehicle. It`s everything a Vehicle needs to be, but more, its specialized. Size, seating, number of wheels, windshield, whatever. It`s not just the difference between 2 Cars, the difference isn`t only the color or whatever.

Inheritance isn`t limited to one level (When is anything, in coding? Goddamn recursion, man.), you can have a subclass of a subclass. We can now have a SportsCar if we want, as an extension of Car, with it's own specializations. One important limitation to keep in mind, though, is that a subclass can only have 1 superclass. I can't have, say, a Harvester be a subclass of both a Vehicle and FarmEquipment. Yech, think of the insurance rates you'd get on that thing. Worse if your harvester was red, I'd imagine. Anyway, tangent! Moving on.

This is all very awesome, but what does it mean for particular objects? If, say, an object, myCar, is an instance of a subclass, Car, then it'll contain all the variables and methods as available in Car, as well as Vehicle. On top of this, any methods declared by any class can be called for objects of the subclass (myCar), providing they are public. This remains true however far the line you go.

There's apparently quite a few complex rules we'll be subjected to soon, so I think it's time for me to understand the core idea fully before moving on, that's it for now..Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!

    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 


  1. I learned something new. :D

  2. That was pretty well explained. Even kids could understand those concepts if you explain em like that :)

  3. you must be a professor or something, you write pretty good.