Interfaces Multiple Inheritance Java


Java Interfaces Multiple Inheritance

Multi-level Inheritance with Two Interfaces

A programmer should be a little careful in using inheritance between two interfaces. It must be "extends" keyword between two interfaces.

interface Reptiles
{
  void size();
}
interface Crocodile extends Reptiles
{
  void amphibian();
}
public class Features implements Crocodile
{
  public void size()
  {
    System.out.println("Reptiles are of small size except crocs and a few snakes and turtles");
  }
  public void amphibian()
  {
    System.out.println("Croc is amphibian");
  }
  public static void main(String args[])
  {
    Features f1 = new Features();
    f1.size();
    f1.amphibian();
  }
}

Interfaces Multiple Inheritance
Output screen of Features.java

Observe the usage of "extends" between the two interfaces Reptiles and Crocodile. The abstract methods of the both are overridden by the Features class. Observe the figure representing above classes.

Interfaces Multiple Inheritance

Multiple Inheritance with Interfaces

There is another way of using interfaces. Interfaces support multiple inheritance. That is, after "extends" keyword, there can be any number of interfaces. This is not possible with abstract classes and concrete classes.

interface Hello1   {  void display1();   }
interface Hello2   {  void display2();   }
interface Hello3 extends Hello1, Hello2      // notice, after extends two interfaces
{  
  void display3();   
}
public class StrangeInheritance implements Hello3
{
  public void display1()  { System.out.println("Display 1");  }
  public void display2()  { System.out.println("Display 2");  }
  public void display3()  { System.out.println("Display 3");  }

  public static void main(String args[])
  {
    StrangeInheritance si1 = new StrangeInheritance();
    si1.display1();	si1.display2();	si1.display3();
  }
}

Interfaces Multiple Inheritance
Output screen of StrangeInheritance.java

Interface Hello3 extends two interfaces, Hello1 and Hello2. The concrete class StrangeInheritance implements Hello3 and overrides the abstract methods of all the three. Even if one is omitted, it is a compilation error. That is, among interfaces, we can go for multiple inheritance. Following figure gives the details.

Interfaces Multiple Inheritance

The above inheritance can be achieved as follows also.

       public class StrangeInheritance implements Hello1, Hello2, Hello3

Getting Objects of Interfaces

This topic is discussed in Object Casting and Java Dynamic Binding Dynamic Polymorphism.

30 thoughts on “Interfaces Multiple Inheritance Java”

  1. Vijayalakshmi D

    Sir… .in abstract class Variables can be any access specifier including private….if I using private with variable in super class(abstract class)…how can I access in sub class or call

  2. Hi Good Day to you all ..

    I need some clarity with this lines.. can anyone please help me with sample lines for the followings:

    Interface reference variable can be assigned with concrete subclass objects. Once assigned, the interface reference variable works like an object. This feature applies to abstract classes also.

    1. interface Test
      {
      public abstract void display();
      }
      public class Demo implements Test
      {
      public void display()
      {
      System.out.println(“Hello Sir”);
      }
      public static void main(String args[])
      {
      Test t1; // reference variable of an interface
      // t1.display(); // error
      Demo d1 = new Demo(); // subclass class object
      t1 = d1; // subclass object assigned to super class reference variable
      t1.display(); // now t1 works practically as an object
      }
      }

  3. Sir I am Confusing in a doubt, If there are 2 interface having same method, The class which implements both interface, and override the method, than How can we know which Interface method the class is Overriding.

    interface A
    {
    public void m1();
    }
    interface B
    {
    public void m1();
    }

    class Demo implements A, B
    {
    public void m1() // How can we know this m1() is of which interface
    {

    }
    }
    ________________________________________________________
    Please Sir, Solve this problem as soon as possible, I have to face an Interview Tomorrow.

    Thanks

    Ankit

  4. Sir,
    What is the advantage of using Interference classes, anyway we will give method definitions in the sub classes. so, what is the point of using an interface classes and extending them?

  5. Sir,can we use same method name in two interfaces and if we implement both these interfaces in one class then which method will be executed? and why? Since both interfaces have same mehod name what about ambiguity??

  6. Hello Sir,

    i am a fresher or beginner in java.

    Please tell me,

    1. When to use Interface instead of Abstract class with an example.

    1. When to use Abstract class instead of interface with an example.

    1. In inheritance, if the super class would like to give all methods with implementation (body), prefer a concrete class. If the super class would like to give a few methods with implementation, prefer abstract class and if the super class would like to not give implementation for all methods, prefer interface.

    2. 1. if u want to write instance variable, static variable & constructor, go with abstract class.
      2.if u want to write abstract method & concrete methods then go with abstract class
      3. if u want to write abstract method only, both are ok but prefer interface because of multiple inheritance.
      4. and if u want to write static variables & abstract method only, then go with interface bcoz of multiple inheritance.

  7. Sir, firstly what is default access specifier in java.
    e.g class abc{}
    what is the access specifier of class abc?

    secondly, i didn’t get what does it mean?
    All interfaces methods should be overridden with public only as OVERRIDDEN METHOD CANNOT HAVE WEAKER ACCESS SPECIFIER.

    1. Default specifier classes are accessible by the other classes of within the package only.

      Overridden methods cannot have less scope access specifier. That is, if in the super class a method is protected, the subclass should override with either public or protected but not by default or private.

  8. thanks a lot.. i understud it well.. i want to hav another example of how multiple interfaces having same method can be inherited..
    lik as in ur example, if both suzuki and ford had method body, wat to do??it gives error???

  9. Sir.. Can u please tel me exactly when to use abstract classes n when to use interfaces.. As far as my knowledge interfaces r used when there s frequent modification s involved n abstract classes r used when v know something in advance..

  10. Sir, how can we say that java supports multiple inheritance through interfaces?
    Can you explain in detail?B’coz there is no link between inheritance and interfaces, I’m bit confused in this.

  11. Can anybody tell me what is partial implementation of interface in java. I am very confused. I think we can implement the methods of our choice and let the others as it is. Can we do this? If yes, How?

Leave a Comment

Your email address will not be published.