5.3 What is inheritance?
What is inheritance?
In OOP, we say that an object inherits the properties and methods of its class. What this means, is that each instance of a class contains a copy of all the instance variables and methods of its class. Just by knowing what the class of an object is, you know quite a lot about the object. If you don't know what a DUKW is, but I tell you that it's a kind of automobile, then immediately you know that it has wheels, gears, a steering wheel, and so on.
OOP takes this a step further though, because it allows some classes to be defined in terms of other classes. For example motorcycles, buses, trucks and armoured cars are all kinds of automobile. We say that motorcycles, buses, trucks and armoured cars are subclasses of the automobile class; the automobile class is the superclass of motorcycles, buses, trucks and armoured cars.
What distinguishes a subclass from its superclass? Automobiles all have certain attributes and functions in common - they all have gears, brakes, wheels, and can all brake, steer, change the engine rpm and change gear. The superclass defines all the variables and methods that are common to all its subclasses, and each subclass defines those that are particular to the subclass. In other words, a subclass can add some extra or different functionality from that which it inherits from its superclass.
For example, an articulated truck has a trailer and some extra gears which the average pickup truck doesn't. Subclassing allows code to deal with this situation by providing instance methods that are found in the subclass only, and by allowing the methods of the subclass to override the methods of the superclass. In other words, in the
ArticulatedTruck class a new
changeGear() method can be defined, which replaces the same method inherited from the
Automobile superclass. This is necessary because of the extra gears and the way in which they are used in articulated vehicles - a method is required to handle the extra functionality.
changeGear() method of the automobile class and the
changeGear() method of the articulated truck class now have the same name, which would cause confusion in the computer. This is why the methods of a subclass override the methods of a superclass, and in fact this is achieved by giving the two methods the same name. So, if the subclass defines a method that has the same name as a method it has already inherited from its superclass, the new method replaces i.e. overrides the superclass method, but only in the subclass. This means that the superclass cannot replace its own method with the method of the same name that is defined in the subclass. This stops any confusion, because as we discussed before instance methods can only be called on the actual instance of which they are a part. In this way, different subclasses can't confuse the issue by mistakenly calling a method with the same name that exists in another subclass of the same superclass. In other words, because the method is only overriden in the subclass, the superclass can't inherit the overriden methods from the subclass, thereby preventing any other subclasses from using the wrong version of the method.
A hierarchy of subclasses can be built that is as many layers deep as it needs to be, and generally speaking the lower down the hierarchy the subclass is, the more specialised its behavior becomes. Armoured cars can be of more than one kind - banks use different armoured cars from those used by the military, which may have a cannon for example. So below the
ArmouredCar subclass two lower subclasses of
BankArmouredCar might need to be defined.
Java has an Object class, and all objects are subclasses of the Object class; therefore all objects inherit certain methods from the Object superclass - including those objects that you define yourself in your Java code.
Inheritance offers the following benefits:
- Subclasses provide specialized behaviors from the base of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times, in many different applications. This greatly reduces application development time.
- Programmers can implement superclasses called abstract classes that define "generic" behaviors. The abstract superclass defines and may partially implement the behavior, but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.