Multiple inheritance in java

Multiple inheritance in java can be achieved by following ways:

  • A class can implements multiple interfaces.
  • An interface can extends multiple interfaces.

Example

interface DisplayAge{
          void displayAge(int age);
}
 
interface DisplayName{
          void displayName(String name);
}
 
public class Test implements DisplayAge, DisplayName{
 
                @Override
                public void displayAge(int age) {
                        System.out.println("Age = " + age);
                }
 
                @Override
                public void name(String name) {
                       System.out.println("Name = " + name); 
                }                               
 
                public static void main(String args[]){
                        //object creation
                        Test obj = new Test();
 
                        //method call
                        obj.name("Shivanshu");
                        obj.age(27);
                }
}

Output

Name = Shivanshu
Age = 27

Java interview questions on Inheritance