Multiple inheritance can be defined as the single class inherited from multiple classes but there is no multiple inheritance in Java. The goal of Java designers is to create a simple programming language; the designers felt that using multiple inheritance in Java will make it too complex. The use of multiple inheritance in Java may lead to diamond problem.
What is Java Diamond Problem?
Consider that there are four classes with name Alpha, Beta, Charlie and Delta respectively. Now let us consider that the classes Beta and Charlie are derived from the Alpha and Delta Class is derived from the classes Beta and Charlie classes using multiple inheritance. The problem with this inheritance hierarchy is when we instantiate the delta class. All calls to method definition in Class alpha will be unclear because the compiler won’t be sure about which class to call whether it is beta class or charlie class. This problem is called as diamond problem.
- Read: Java serialization
Interface is the alternative method for Multiple Inheritance in Java.
Now we know that multiple inheritance is not possible in Java due to the diamond problem. Multiple interface is used as an alternative method for multiple inheritance. The work of interface is quite similar to the multiple inheritance. The operation of interface is similar to single inheritance. Through this simplified process of interfacing diamond problem is eradicated because the compiler won’t be stumped.
Multiple inheritance also complicate the process like casting and constructor chaining etc. There won’t be a lot of scenario in which you require multiple inheritance in Java. Many believe that the Java designers avoided inheritance for simplicity but the designers claim that inheritance was not used because to include multiple interface. Multiple interface have method declaration and does not provide any implementation.
- More: Java interview Q&A available here
The following program will act as an example for interfacing in Java
interface K
{
public void myFirstMethod();
}
interface V
{
public void myFirstMethod();
}
class Demo1 implements K,V
{
public void myFirstMethod()
{
System.out.println(” The Multiple inheritance sample using interfaces”);
}
}
In the above program the class is implemented using two interfaces. One class can be implemented at any number of interfaces. Interface and inheritance have no enigma in between them because both use same method.
What is default method in Java Interface?
A new method will be implemented in Java classes which in turn will implement the interface. When Java programmers complained that this process is difficult, Java 8 came with a new option called default method.
Default method method during interfaces are enforced by default but without overriding the implementing classes.
Let us consider an interface Matrix in which you want to add a new method called Kar(). If the method Kar() is added using the old interface technique, then calls which implement the interface “Mat1” will also be changed. So we are declaring Kar() as a default method.
public interface Mat1
{
default void Kar()
{
System.out.println(“This world may be unstable”);
}
}
If the classes which implements the matrix interface don’t need change itself until you want to override the Kar(). All classes can directly call instance.Kar() method.
public class Hum1 implements Mat1
{
public static void main(String[] args)
{
Hum1 trin1= new Hum1();
//Call default method using instance reference
trin1.Kar();
}
}
How to gratify Multiple Inheritance using default method?
Until Java 7 inheritance is supported by the keyword “extend” which was used to create a child class from parent class but cannot be extended between two class. Interfaces were only declaring contacts in Java 7 which implements classes. No specific characteristics are available with interface which classes can inherit. So even a class implement an interface it can’t be called as Multiple inheritance. The default method can interface the behaviour as well.
If a class can implement two behaviour and both can be defined by default methods, it becomes essential to inherit behaviours from two class which is called as multiple inheritance.
In the program given below the class Matrix does not define the behaviour of the class but inherit from the parent class.
interface Mat1
{
default void Mot1()
{
System.out.println(“Hello, Let us move”);
}
}
interface Fly1
{
default void Fly1()
{
System.out.println(“Flying High!!”);
}
}
public class Hum1 implements Fly1, Run1
{
public static void main(String[] args)
{
Hum1 him= new Hum1();
him.Mot1();
him.fly1();
}
}
- Read: Java access modifiers
Conflicts that are possible during Interfacing:
When these interfaces declare two method there won’t be any problem what will happen if these interfaces decide to define a method in same name. The problem is which method will be invoked when the human instance is called in the following program.
interface Mat1
{
default void run1()
{
System.out.println(“Ajith, I’m flying “);
}
}
Interface Run1
{
default void run1()
{
System.out.println(“You can run, Karthikh”);
}
}
public class Human1 implements Fly1, Run1
{
public static void main(String[] args)
{
Human1 him= new Human1();
//What will happen when below statement will execute
//him.run1();
}
}
In the above program the caller class will decide which run() method will be invoked using the reference of interface like given in the example:
Mat1.super.fly1(); //Call Mat1’s fly1() method
//or
Run1.super.fly1(); //Call Run1’s run1() method
That’ how you will solve the multiple inheritance problem in Java 8 using the default method.