Posted by: lrrp | January 28, 2015

Multiple Inheritance in Java

Multiple Inheritance in Java

Multiple inheritance is the capability of creating a single class with multiple super classes. Unlike some other popular object oriented programming languages like C++, java doesn’t provide support for multiple inheritance in classes. Java doesn’t support multiple inheritance in classes because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritance.

Diamond Problem

To understand diamond problem easily, let’s assume that multiple inheritance was supported in java. In that case, we could have a class hierarchy like below image.

diamond-problem-multiple-inheritance

Let’s say SuperClass is an abstract class declaring some method and ClassA, ClassB are concrete classes.

SuperClass.java
1
2
3
4
5
6
package com.journaldev.inheritance;
public abstract class SuperClass {
    public abstract void doSomething();
}
ClassA.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.journaldev.inheritance;
public class ClassA extends SuperClass{
    
    @Override
    public void doSomething(){
        System.out.println("doSomething implementation of A");
    }
    
    //ClassA own method
    public void methodA(){
        
    }
}
ClassB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.journaldev.inheritance;
public class ClassB extends SuperClass{
    @Override
    public void doSomething(){
        System.out.println("doSomething implementation of B");
    }
    
    //ClassB specific method
    public void methodB(){
        
    }
}

Now let’s say ClassC implementation is something like below and it’s extending both ClassA and ClassB.

ClassC.java
1
2
3
4
5
6
7
8
9
10
package com.journaldev.inheritance;
public class ClassC extends ClassA, ClassB{
    public void test(){
        //calling super class method
        doSomething();
    }
}

Notice that test() method is making a call to superclass doSomething() method, this leads to the ambiguity as compiler doesn’t know which superclass method to execute and because of the diamond shaped class diagram, it’s referred as Diamond Problem and this is the main reason java doesn’t support multiple inheritance in classes.

Notice that the above problem with multiple class inheritance can also come with only three classes where all of them has at least one common method.

Multiple Inheritance in Interfaces

You might have noticed that I am always saying that multiple inheritance is not supported in classes but it’s supported in interfaces and a single interface can extend multiple interfaces, below is a simple example.

InterfaceA.java
1
2
3
4
5
6
package com.journaldev.inheritance;
public interface InterfaceA {
    public void doSomething();
}
InterfaceB.java
1
2
3
4
5
6
package com.journaldev.inheritance;
public interface InterfaceB {
    public void doSomething();
}

Notice that both the interfaces are declaring same method, now we can have an interface extending both these interfaces like below.

InterfaceC.java
1
2
3
4
5
6
7
8
package com.journaldev.inheritance;
public interface InterfaceC extends InterfaceA, InterfaceB {
    //same method is declared in InterfaceA and InterfaceB both
    public void doSomething();
    
}

This is perfectly fine because the interfaces are only declaring the methods and the actual implementation will be done by concrete classes implementing the interfaces, so there is no possibility of any kind of ambiguity in multiple inheritance in interface.

That’s why a java class can implement multiple inheritance, something like below example.

InterfacesImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.journaldev.inheritance;
public class InterfacesImpl implements InterfaceA, InterfaceB, InterfaceC {
    @Override
    public void doSomething() {
        System.out.println("doSomething implementation of concrete class");
    }
    public static void main(String[] args) {
        InterfaceA objA = new InterfacesImpl();
        InterfaceB objB = new InterfacesImpl();
        InterfaceC objC = new InterfacesImpl();
        
        //all the method calls below are going to same concrete implementation
        objA.doSomething();
        objB.doSomething();
        objC.doSomething();
    }
}

Did you noticed that every time I am overriding any superclass method or implementing any interface method, I am using @Override annotation, it’s one of the three built-in java annotations and we should always use override annotation when overriding any method.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: