How to create interface in java
How to create interface in java

Does the Java support multiple inheritances?No, Java does not support multiple inheritances but there is always an alternative approach for everything that the Java does not support.For multiple inheritance, Java has an approach called “Interface”.

Interface java

What is Interface?We can define the interface as “A means of achieving full abstraction and multiple inheritances”.An interface contains constant values and method declaration.The main difference of class and the interface is that the interface only contains the method declaration neither the method definition nor instance variable.

Java interface example

Following fig. shows the simplest example of multiple inheritances:

Here class Vehicle will display the mode of moving of class Boat or Car.But the compiler will get confuse which class move() to call.This is the reason that the java does not support multiple inheritances.To overcome this the above can be written programmatically as follow:

Interface Mode{

 protected void move();

}

class Car implements Mode{

move(){

    System.out.println(“On road”);

  }

}

class Boat implements Mode{

move(){

    System.out.println(“On water”);

  }

}

class DisplayMove{

   public static void main(String args[]){

   Car c=new Car();

  Boat b=new Boat();

  c.move();

  b.move();

  }

}

Thanks for reading, you can share this article with your friends.