Monday, August 24, 2009

Java Inheritance - Explained

Today, in this blog post, i am going to try to explain the concept of inheritance in Java. There are many people who are in a dilemma when it comes to explaining about the inheritance model of Java especially when they come across interfaces. The most common doubt that how does Java claim that it does not implement multiple inheritance when instead we use classes that inherit other classes and implement multiple interfaces.

So, this blog post intends to answer the following questions
  1. What is the difference between implementing an interface and extending a class?
  2. Why can we say that implementing an interface is not a concept related to inheritance?

First of all one need to know that there is a huge difference between the multiple inheritance model of java and that of C/C++.

One of the primary reasons why the multiple inheritance model was removed from the Java language was because it eliminated one of the most notorious problem caused due to multiple inheritance - The Diamond Problem. If you dont know about it, I suggest that you google it once you have read this article.

Whenever we speak of inheritance in Java the first thing that must come to one's mind must be - Classes. Not interfaces. Because inheritance in java is strictly constrained to Classes. Since java does not allow a class to have more than one superclass, hence it leaves no doubt that multiple inheritance is not supported in java.

Then how do we explain the case when a class extends one base class and implements one or more interfaces? Is that not multiple inheritance? Well, I must say that thought this might look like multiple inheritance, it certainly is not.

Let me illustrate this idea with an example.

Suppose there is a class 'Mammal' and a class 'Human'. If you were to relate these two classe, how would you relate them? Of course, the answer is plain and simple(as all answers are!) Mammal is the parent class of Human. Programatically, we could write this relationship in java as the following code :

class Human extends Mammal{
...
...
...
}


All of us know that humans are an intelligent species(Though sometimes i am not so sure about that) Now, supppose we had a class called Intelligent that had a function called thinkStupidThings() ,how would you relate it to Human? Of course, you would say- Human is Intelligent.

If java were like C/C++, then we could have easily written the following code

class Human extends Mammal extends Intelligent{
...
...
...

thinkStupidThings(){...}

}

BUT! Java is NOT C/C++. So the above code is not valid and our intelligent Java compiler will throw a compile time error if you try to compile this.

So, now the question is, How do we depict this relationship in Java? Dont worry folks, this is where Interfaces come to our rescue. Well, why Interfaces? And how to decide which one of them needs to be made an interface?

Lets analyze our problem clearly. We have Mammal,Human and Intelligent. Now if we notice carefully, we see that no matter what happens, all humans are mammals. But the same cannot be said about Intelligent. What i mean is that intelligence is a characteristic of not only Humans but also many other species like dolphins and apes. Some of them are even more intelligent than humans(Had we been more intelligent, we would have solved the mystery of the universe by now!).

So, now as we see, the problem is that we have tried to tie down intelligence to Humans and have been very unjust in not acknowledging the intelligence of other species. Thus after careful observation, it seems like a better option to use Intelligence as an interface because it is more like a characteristic feature in humans. So, a much better way of denoting this relationship in Java would be like this :

class Human extends Mammal implements Intelligent
{
...
...
...
thinkStupidThings(){...}
}

where the thinkStupidThings() function is declared in the Intelligent interface. This way each class has to explicitly define the thinkStupidThings() function from its own point of view. Thats useful because i believe that both humans and dolphins have a very different perspective of stupid things.

So, deciding what is supposed to be an interface and what should be a base class is a design consideration and should be looked into carefully depending upon your business requirements.

As far as the question of implementing multiple interfaces come, lets assume that we have interfaces like Footballer, Cricketer,Artist,Moveable,Nocturnal etc etc. The human class can choose to implement all of these interfaces or any combination of them. So, if i were to make a joke out of it, i can say that you can choose to become a Footballer or an Artist or any other profession by simply implementing the appropriate interface and still somehow manage to remain a Mammal! Isin't that great? Is sure is. Oh, how i wish it were that easy in real life!

Well, thats all that there is to it. At least thats how i felt like explaining it. (lol)
Hope that you find it useful.

Happy Programming :)

Signing Off
Ryan