Learn how to Create interface in java

In this article we will learn how to Create interface in java. So, lets start – 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, it an approach called ‘Interface‘.

Create interface in java

Now we will discuss the topic in detail, first of all we will learn what is in A means of achieving full abstraction and multiple inheritances. 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.

An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.

Following fig. shows the simplest example of multiple inheritances:

Interface in Java
How to 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:

See Also  Become Pro Coder in Java Programming

Example:

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 and Learn Interface in Java Programming Language. A class that implements an interface must implement all the methods declared in the interface. The methods must have the exact same signature (name + parameters) as declared in the interface. The class does not need to implement (declare) the variables of an interface. Only the methods.

Create interface in java Code
Create interface in java Code

In this article we will learn how to Create interface in java. So, lets start – 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, it an approach called ‘Interface‘.

See Also  Download Java and install JDK

Create interface in java

Now we will discuss the topic in detail, first of all we will learn what is in A means of achieving full abstraction and multiple inheritances. 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.

An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.

Following fig. shows the simplest example of multiple inheritances:

Interface in Java
How to 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:

See Also  How to Create a website using Java

Example:

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 and Learn Interface in Java Programming Language. A class that implements an interface must implement all the methods declared in the interface. The methods must have the exact same signature (name + parameters) as declared in the interface. The class does not need to implement (declare) the variables of an interface. Only the methods.

Create interface in java Code
Create interface in java Code
79205e8930c058706ba2b07f282252c5?s=96&d=mm&r=g
Aasim Sheikhhttps://syncsas.com/
CTO at SyncSaS Technologies

More from author

Related posts

Advertismentspot_img

Latest posts

How to increase YouTube Subscribers 2 Organic Strategy

How to increase YouTube Subscribers Being a YouTube personalities everyone wants the number of channel subscribers should multiply very quickly. Making YouTube video and uploading...

5 Ways to Increase Your YouTube Views

Increase Your YouTube Views Try going back and editing it to make it even better, and work on new videos. A helpful tip is to...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!