Object Oriented Programming – Inheritance in detail

Introduction

This article is in relation to the interview questions asked to the java candidates in particular. I have been interviewing candidates and I find them very confused between the features of OOP. Most of the time people are not very clear about Inheritance. We will discuss this in detail. To read more about my articles on java and it’s concepts, please visit Java Articles.

Purpose of the article

This article is for the novice and the experts as well, I always believe that the concepts are something which has to be read and brushed up again, here we will discuss Inheritance in detail, this will lead to interesting consequences called Polymorphism and we can categorize them as static and dynamic polymorphism or compile time or run time polymorphism.

Defining OOP

OOP stands for Object Oriented Programming, which is not so complicated to understand. We have to adapt to this method to make programs more realistic and close to the real world. We use programs to solve a real world problem, so it would be really nice if we can have a programming technique which enables us model all our real world objects into programming model. That is the reason for the name Object Oriented Programming. Everything here is an object and we have to analyze various features and aspects of an object.

Object

An object is a programming model of the real world entity. For every possible entity around us we can create a Model object. We can also have a parent child kind of relation between our model objects.

Now these objects have two fundamental features, they are properties and behavior similar to the real world object, for e.g. if you create a Car object, it will have properties like speed, color, registrationNumber etc. It will have behaviors like accelerate, stop etc.

It is simple to remember that all the nouns are properties and all the verbs are behaviors. For e.g. color is a noun and accelerate is verb.

When we talk about the parent child relations between two objects, we necessarily have to take into account the possibility of inheritance. Inheritance fundamentally means retaining the features and behaviors of the parent.

Inheritance in detail

When a class B inherits the features and(or) behavior of another class A, we say that the class B is a child of class A. In java this relation between two class is enforced by using the keyword extends.

What do we achieve by inheritance
Inheritance is the heart of OOP, this is what enables re-usability. Re-usability of properties and behaviors. Another question comes into mind, why do we inherit if we want to re-use the attributes of the parent class?

The answer is really very simple, no one is satisfied with the available stuff, everyone needs to improve upon it. Whatever may be the reason, we always try to improve things, give special features to it. Think it in this way, a person is capable of computer programming but he never learnt car driving. Now he has a son, who can inherit all the features from his father, so he knows computer programming and he also wants to learn car driving.

What are the ways he could know car driving?
Either his father knows it and he inherit or he learns himself. That is the need of inheritance, if we want a superior species with special features then we extend the parent, inherit its attributes and introduce new attributes (like the car driving is introduced in the child above).

This is all about inheritance, now the interesting thing is what are the side-effects or consequences of inheritance, what all we need to take care while using inheritance?

To answer this question properly, we need to know what all we can inherit. For now let us assume that we can inherit everything from a parent class. This will include:
1) properties
2) behavior (methods)

About inheriting properties

Inheriting properties means that the child class can access the parent properties as if the properties are from the child class. There is one restriction though and the restriction is that it cannot access the private properties (that makes sense , a child must not access the private properties of the parent). So apart from the private properties rest all type of properties are inheritable. This means a child class can inherit the following:

  1. properties with protected access modifiers.
  2. properties with no access modifiers.
  3. properties with public access modifiers.

As a matter of fact, any property of a parent class which has a public access modifier is available to the child class as an inherent property at any place.
Also, any property of a parent class which has a protected access modifier is available to the child class as an inherent property at any place.
And any property of a parent class which has a no access modifier is available to the child class as an inherent property only if the child class is in the same package as the parent class.

Note: Just to make it clear, when I say a property is available to the child class as an inherent property, I mean you can access the property directly using the this operator of the child class.

The below code helps us to understand the above discussion:

In the above code we have the Parent class with the four properties:

  • private id
  • protected color
  • public name
  • category

The class Child is in the same package, so it can access all the properties except the private id using the this keyword. This proves that the three properties are inherent to the Child class in the same package.
The class ChildInDifferentPackage is in a different package, so it can only access the properties which are declared either as public or protected and not the ones which have private or no(i.e. package default) access modifiers. Hence the protected and public properties are inherently available to the child in a different package.

Note: Point worth noticing is that, if we declare new properties in the child class with the same name as the properties in the parent class. Then if the properties are accessed with the child class reference or the this keyword in the child class will not allow to access the parent class properties. To access the parent class property we need a parent class reference.

For more explanation check the source code below:

Output of the above code

With Child reference : GREEN CHILD_CATEGORY CHILD
With Parent reference: RED PARENT_CATEGORY PARENT
With this reference : GREEN CHILD_CATEGORY CHILD

In the above code snippets, the child class defines it’s properties with the same names as the parent class, so if it tries to print the properties with the child reference or the this reference, it will print the property values of the child class. If it uses the parent class reference, then the parent property values are printed.

About inheriting behavior

Behaviors are represented by methods in OOP. When we talk about inheriting behaviors, we mostly talk about inheriting methods. In most of the cases the above access and inheritance rule remains same. The child class (if in the same package) can inherit all the methods except the ones declared with a private access modifiers. The child classes in the different package will only inherent the public and the protected methods.

What happens when the child also has the same methods as the parent class?
The answer is simple, the child class reference will call the child class methods, the parent class reference will call the parent class methods and the this reference will call the child class methods.

This is very obvious as we discussed in the the previous section. Now there is just one more case we need to take care for, what if the Child class instance is created and assigned with the Parent class reference. Something like the below:
Parent p = new Child()
Highlighting again, this is only when parent and child class has the same methods.

In this case the method called will be the one in the child class, reason is that the object is resolved to be a child class object. This is called runtime/dynamic polymorphism. The name runtime is because the JVM will know which method to call only at the runtime.

Polymorphism
The term has a broad meaning, it means having/exhibiting many forms. To understand this, we can define two methods with the same name and different input parameters in the same class.

Below is the code for the same:

The above two methods are called polymorphic methods. In this example both the methods are in same class and they are overloaded, the name overloaded suggests that there is an extra load on the name, the name is is serving two methods.

There can be another situation as below:

This is also considered as overloading, well you may ask that where is the other method which is being overloaded. If we remember the relationship between a child and a parent class, then all the methods which are protected are inherent to the child class. So the protected method abc() from the Parent is already present in the Child class, and we are writing another method with the same name and different argument i.e. abc(String a) so it is a valid overload.

Overriding

We again trace back our discussion to the section with heading What happens when the child also has the same methods as the parent class?. Below source code will be helpful in understanding the same.

Is this overloading?
No this is not, overloading is when the methods have different arguments. This is something else, and in OOP we call it overriding. Overriding is another consequence of inheritance, this is a result of one method co-existing in the Parent as well as the Child class.

So, now we have answer to the question, what happens when a child class object is created and assigned to a parent class reference? As the object is from child class, it will be calling the child class method. This is called run time polymorphism, the reason is that the JVM will choose the qualifying method at the run time depending on the object type and not on the reference type.

So the below code will print the following values:

Following is the output of the above code:

In child abc
In child abc
In parent abc

This is all about inheriting behaviors. Now as we have already started the discussion about Polymorphism, I would like to discuss one more thing before wrapping this article.
What all can be polymorphic, is it just the methods?

The answer is NO, as polymorphic means many forms hence the below two statements give the answer:
Parent c1 = new Child();
Child c2 = new Child();

If we observe it closely, the two reference variables are are basically representing the same object or same type of objects, so we can say that these two are polymorphic. That is another way we can establish that Polymorphism is a consequence of Inheritance. Polymorphism here means an object can be referenced by a the Parent class reference, Child class reference or the Interface reference.

Conclusion

Here we learnt the core of OOP which is inheritance, we also learnt what all may result while implementing inheritance and we also learnt the role of access modifiers in inheritance.

Hope this helps, happy reading.