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
- Why multiple inheritance is not supported in java?
- How to implement multiple inheritance in java?
- Are interfaces also inherited from Object class?
- Why an interface cannot have constructor in java?
- How do you restrict a member of a class from inheriting to it’s sub classes?
- Can a class extend itself in java?
- Are constructors inherited in java?
- What happens if both superclass and subclass have a field with same name?
- Are static members inherited to subclasses in java?