Interfaces Multiple Inheritance Java

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.

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.

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.

Getting Objects of Interfaces

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

29 thoughts on “Interfaces Multiple Inheritance Java

  1. Surendrkumar

    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. S. Nageswara Rao, Corporate Trainer

      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
      }
      }

  2. Ankit

    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

  3. keshore

    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?

  4. Snehal

    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??

  5. Vikrant Chavan

    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. S. Nageswara Rao, Corporate Trainer

      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. sandeep

      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.

  6. Kapil

    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. S. Nageswara Rao, Corporate Trainer

      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.

  7. rifathfathima66@gmail.com

    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???

  8. Praveen

    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..

  9. Shiv

    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.

  10. Manzor Ahmad

    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 Reply

Your email address will not be published. Required fields are marked *