Subscribe for notification
Categories: 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:

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.

This post was last modified on August 7, 2020 5:49 pm

Sponsored
Aasim Sheikh

CTO at SyncSaS Technologies

Share
Published by

Recent Posts

Adsense Updated Dashboard

Changes in Adsense Dashboard Recently you might have seen few changes in Adsense dashboard, these…

3 weeks ago

Guide Book For Google Adsense

With my experience in working as AdSense publisher, I have written many articles, tutorials for…

3 weeks ago

How to manage Google Adsense to get High CPC

Manage Adsense for High CPC CPC (Cost per click) in simple words the CPC is…

3 weeks ago

Google Adsense Introduce page level ads

If you are already the publisher of Google Adsense account then now its time to…

3 weeks ago