Java:The Pitfalls of Inheritance Part-1

In Object Oriented Programming, inheritance is a commonly used mechanism to model the relationship between two types. However, modelling such relationship without realizing the impact on the overall application may result in unexpected problems. In this article, I will try to present some internal details of inheritance mechanism based on the Java language and highlight some of the problems that a developers should be aware of when using inheritance of types.

Inheritance Defined

One of the first questions that we should answer is what do we mean by inheritance. The inheritance is a modelling technique of expressing the relationship between a generalized type and a specialized type. This in essence is a way of expressing IS A relationship. For example, we can say Car IS A Vehicle. In such situations, we will say Car inherits from Vehicle. In the world of Java language, this inheritance relationship is expressed with the following semantics.

In this model, Car is a subclass of the Vehicle super class. Now the next question is what we inherit. By default, the subclass inherits all the interfaces and the implementations from the super class. Note that, the use of the word “interface” in this context does not refer to the interface type specific to the Java language. In OO, the “interface” is a function/method that a particular class exposes to the external world. Coming specific to Java, the inheritance relationship guarantees the following:

  • The sub class inherits all the public methods and the implementation in the super class.
  • The sub class inherits all the protected methods and their implementation in the super class.
  • The sub class also inherits all the public and protected member variables from the super class.
  • However, constructors are not part of this inheritance model. We shall also see later the invocation relationship of constructors that exists between a sub class and its immediate super class.

These consequences of inheritance also imply the following things that are vital to how we can use a super class and any sub class of it. In general, when you model and implement an inheritance relationship, you guarantee the following:

  • The sub class is capable of accepting all the messages that the super class accepts.
  • The sub class can replace the super class anywhere the super class is called for without affecting the final outcome.

Modifying the Super Class Behavior

With inheritance mechanism, although the sub class by default inherits all the interfaces and implementations of the super class, it is possible to override any of the implementations in the sub class. It is also perfectly possible to extend the interfaces by adding new methods to the sub class. Both of these are mechanisms to modify or extend the functionality of the super class. For example, consider the following inheritance scenario in Listing 1.

Listing 1: The Vehicle-Car interface inheritance

In this example, we have modelled Car IS A Vehicle. The Car class overrides the stop() method implementation of its super class Vehicle. However, it decides to reuse the accelarate() implementation from its super class.

As a result of this inheritance relationship, we will be able to write a VehicleTester class in the following form.

Listing 2. The example benefits of using inheritance relationship

In this example, you can immediately see the benefits of inheritance relationship. We could write a single method accepting a super class type and we are able to pass any sub class to the same method. This greatly reduces the strongly typed nature of code and increases the flexibility of the application module.

Restricting inheritance

Sometimes in your design you would like to stop some classes and methods being inherited. The final keyword in Java achieves this functionality. When you declare a class to be final, no other sub class of it can be created. Similarly, when you declare a method to be final, no sub class can override that method.

Overriding member variables

So far we have talked about how can you override the methods declared in the super class. It is also possible to hide the member variables declared in the super class, by declaring a member variable in the sub class with the same name as in super class. The type of the field does not matter, only the name needs to match. Notice that when you hide a member variable in the sub class, you can still access the super class variable by using the super keyword. This means that the member variables cannot be overridden but can only be hidden. It is important to notice this subtle distinction between overriding and hiding.

The logic behind that Java allows the hiding of member variables, is that you can then create a sub class of any super class without detailed knowledge of its internal implementation. This is more in the line of encapsulation.

How it works

We have just seen how powerful inheritance mechanism is and how relatively it is to implement in real world. We will later in this article examine some of the pitfalls with this inheritance mechanism but for the time being, let us see how the inheritance works in Java.

Object Initialization and Inheritance

When an object is initialized, all the instance variables defined within the class of that object and also all the instance variables defined in its super class get initialized. Consider the following example in Listing 3.