How to create interface in java

0
626
create interface in java
create interface in java
Say Hi! and Follow

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:

create interface in java
create interface in java

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:

create interface in java

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.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 25,221 other subscribers

Leave a Reply