Interfaces Multiple Inheritance Java

Interfaces Multiple Inheritance Java

We know earlier, Java does not support multiple inheritance, basically. But to support multiple inheritance, partially, Java introduced "interfaces". An interface is not altogether a new one for us; just it is a special flavor of abstract class where all methods are abstract. That is, an interface contains only abstract methods.

Syntax of multiple inheritance with two interface

Interfaces Multiple Inheritance

Output screen of MotorCar.java

In the above code there are two interfaces – Suzuki and Ford. Both are implemented by MotorCar because it would like to have the features of both interfaces. For this reason (to have the functionality of many classes) only, Java supports multiple inheritance through interfaces. Just to inform there are multiple interfaces and not classes, tell the compiler by replacing "extends" with "implements". MotorCar, after implementing both the interfaces, overrides the abstract methods of the both – body() and engine(); else program does not compile. Java supports multiple inheritance partially through interfaces. Following figure gives the view of the above interfaces.


Interfaces Multiple Inheritance

Features of Interfaces

Interfaces are derived from abstract classes with a special additional rule that interfaces should contain only abstract methods. Let us see some more properties.

  1. Interfaces support multiple inheritance which is not possible with concrete and abstract classes.
  2. "implements" keyword is used in place of "extends". "extends" comes with concrete and abstract classes.
  3. Interfaces must contain abstract methods only.
  4. As in abstract classes, all the abstract methods of the interfaces should be overridden by the subclass.
  5. As with abstract classes, with interfaces also, objects cannot be created but reference variables can be created.
  6. 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.
  7. As all methods must be abstract and public, these two keyword can be omitted in method declaration. If omitted, JVM takes by default.
  8. All interface variables must be public, static and final. If omitted, they are taken by default.
  9. All interface methods should be overridden with public only as overridden method cannot have a weaker access specifier.

Later you get a table where actual differences between abstract classes and interfaces are given.

Defining an Interface

We know earlier, interface should have only abstract methods (without any body or implementation code). The abstract methods give a template structure of methods from which subclasses can be developed easily. Subclass job is just to override (or give the code) as per its convenience and need not think about the design part (how many methods it should contain, what are they, how they are related etc). Interfaces play a vital role in Java coding and designing modules.

The interface methods can be implemented by various subclasses as per the code appropriate to them. For example, the following "interface Mammal" can be implemented by thousands of mammals as per their nature. Following program is one of the generic implementations.

Interfaces Multiple Inheritance

Output screen of Nature.java

"interface Mammal" includes three methods and one variable. In the method declaration, the keywords public and abstract are omitted as a style and if omitted, we know earlier, they are taken by default. Similarly, the string variable "chambers" is, by default, public, static and final. That is, interface variables cannot be reassigned (as they are implicitly final).

The interface methods offSpring(), feed() and blood(String) can be implemented by different species of animals in different ways as per their nature. The above code is just one example of such implementations.

Compare and Contrast Abstract classes with Interfaces?

Even though the interface is a special derivative of abstract class; it differs a lot with abstract class. Following table gives their differences.

Abstract class Interface
1. Multiple inheritance is not supported

Multiple inheritance is supported
2. To inherit "extends" keyword is used

To inherit "implements" keyword is used
3. May include concrete methods also

All must be abstract methods
4. Methods may be of any specifier except private. That is, may be
public, protected or default

Methods must be public only
5. Access specifier should be written

If omitted, taken by default (as public)
6. Access modifier abstract should be written

If omitted, taken by default (as abstract)
7. Variables can be any access specifier including private

Variables should be public, static and final. If omitted, taken by default.
8. Constructors can be included in code

No constructors
9. main() can be included in code

main() method cannot be included

Table: Differences between abstract classes and interfaces

After knowing the differences, let us see the similarities.

Similarities between abstract classes and interfaces
  1. With abstract classes and interfaces, objects cannot be created; but reference variables can be created.
  2. All the abstract methods of the both should be overridden by the inherited class.
  3. "Dynamic polymorphism" is supported by both.
  4. Both works as template (structure of methods) from which new classes can be derived easily.

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 *