Interface in Java
Interface in Java
https://www.javatpoint.com/interface-in-java 1/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
https://www.javatpoint.com/interface-in-java 2/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
Syntax:
interface <interface_name>{
The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static
and final keywords before data members.
In other words, Interface fields are public, static and final by default, and the methods are public and abstract.
https://www.javatpoint.com/interface-in-java 3/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
As shown in the figure given below, a class extends another class, an interface extends another interface, but a class
implements an interface.
https://www.javatpoint.com/interface-in-java 4/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
In this example, the Printable interface has only one method, and its implementation is provided in the A6 class.
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
Test it Now
Output:
Hello
In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle
classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different
implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who
uses the interface.
File: TestInterface1.java
https://www.javatpoint.com/interface-in-java 5/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
}}
Test it Now
Output:
Lea
drawing circle
Let's see another example of java interface which provides the implementation of Bank interface.
File: TestInterface2.java
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Test it Now
Output:
https://www.javatpoint.com/interface-in-java 6/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
ROI: 9.15
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
Test it Now
Output:Hello
Welcome
https://www.javatpoint.com/interface-in-java 7/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class
because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its
implementation is provided by the implementation class. For example:
interface Printable{
void print();
}
interface Showable{
void print();
}
Test it Now
Output:
Hello
As you can see in the above example, Printable and Showable interface have same methods but its implementation
is provided by class TestTnterface1, so there is no ambiguity.
Interface inheritance
A class implements an interface, but one interface extends another interface.
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
https://www.javatpoint.com/interface-in-java 8/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
Test it Now
Output:
Hello
Welcome
File: TestInterfaceDefault.java
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
Test it Now
Output:
https://www.javatpoint.com/interface-in-java 9/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
drawing rectangle
default method
File: TestInterfaceStatic.java
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Test it Now
Output:
drawing rectangle
27
https://www.javatpoint.com/interface-in-java 10/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
Note: An interface can have another interface which is known as a nested interface. We will learn it in detail in the
nested classes
chapter. For example:
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
« Prev Next »
https://www.javatpoint.com/interface-in-java 11/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
Feedback
intellipaat.com OPEN
RxJS tutorial
RxJS
React Native Python Design Python Pillow Python Turtle Keras tutorial
tutorial Patterns tutorial tutorial
Keras
React Native Python Design Python Pillow Python Turtle
Patterns
Preparation
https://www.javatpoint.com/interface-in-java 12/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
Trending Technologies
Machine DevOps
Learning Tutorial Tutorial
Machine Learning DevOps
B.Tech / MCA
https://www.javatpoint.com/interface-in-java 13/14
10/13/21, 11:22 AM Interface in Java - Javatpoint
https://www.javatpoint.com/interface-in-java 14/14