Java Interface is a reference type. An interface is same as a class. The major difference is that Interface uses the keyword interface instead of class keyword. An interface is an abstract class. It contains abstract methods. Other than abstract methods, an interface also contains constants and default methods. Unlike classes, interfaces do not have constructers.
Java Interface helps achieve abstraction as it contains multiple abstract methods. These methods are overridden in classes. An interface cannot be instantiated that means programmer cannot create objects of an interface.
Create Java Interface
The syntax for creating a Java interface is:
There are some important points to keep in mind about interfaces. The name of .java file should be same as the name of interface. The first letter of the interface name should be in upper case.
The below example shows an interface named as Website:
// interface interface Website { public void websiteName(); // interface method does not have a body public void websiteType(); // interface method does not have a body }
Implement Java Interface
The classes that inherit from the interface use implement keyword instead of extends keyword. Multiple classes can inherit from an interface. The implemented class contains the definition of abstract methods present in the interface. The interface is implicitly abstract so there is no need to write abstract keyword before interface name or method name.
The below example shows how to implement interface using a class.
// interface interface Website { public void websiteName(); // interface method does not have a body public void websiteType(); // interface method does not have a body } class HomePage implements Website { public void websiteName() { System.out.println("Tutorialsart"); } public void websiteType() { System.out.println("Programming Language Tutorials"); } } class Example { public static void main(String[] args) { HomePage obj = new HomePage(); obj.websiteName(); System.out.println("for"); obj.websiteType(); } }
The output of the above code after compilation is:
Multiple Java Interfaces
A java file can have more than one interfaces. Classes can also implement be implemented from more than one interfaces. While declaring an implemented class, the names of interfaces are separated by comma (,). The below example shows the concept of multiple interface implementation.
// multiple interfaces interface InterfaceA { public void firstMethod(); // interface method } interface InterfaceB { public void secondMethod(); // interface method } // ImplementedClass "implements" InterfaceA and InterfaceB class ImplementedClass implements InterfaceA, InterfaceB { public void firstMethod() { System.out.println("method from InterfaceA"); } public void secondMethod() { System.out.println("method from InterfaceB"); } } class Example { public static void main(String[] args) { ImplementedClass obj = new ImplementedClass(); obj.firstMethod(); obj.secondMethod(); } }
The output of the above code after compilation is:
Extend Java Interfaces
Just like classes, interfaces can also be extended from other interfaces. The extend keyword is used to extend interfaces. Extend keyword creates child parent relationship between interfaces. The child interface inherits the methods of parent interface.
Here is another example that explains extended interfaces. In the code below InterfaceA is the parent interface. The InterfaceB extends from InterfaceA and the InterfaceC extends from InterfaceB. The InterfaceB inherits all the abstract methods from InterfaceA and has three of its own methods. So, it has access to 5 methods. The InterfaceC inherits methods from InterfaceB and has four of it own methods. So, it has access to all 9 methods.
The class ImplementedClass implements the interface InterfaceC. The class contains the definition of all abstract methods.
// Extended interfaces // First Interface interface InterfaceA { public void methodA(); public void methodB(); } // Second child interface interface InterfaceB extends InterfaceA { public void methodC(); public void methodD(); public void methodE(); } // Third child interface interface InterfaceC extends InterfaceB { public void methodF(); public void methodG(); public void methodH(); public void methodI(); } // class implements from the child interfaceC class ImplementedClass implements InterfaceC { public void methodA() { System.out.println("method 1"); } public void methodB() { System.out.println("method 2"); } public void methodC() { System.out.println("method 3"); } public void methodD() { System.out.println("method 4"); } public void methodE() { System.out.println("method 5"); } public void methodF() { System.out.println("method 6"); } public void methodG() { System.out.println("method 7"); } public void methodH() { System.out.println("method 8"); } public void methodI() { System.out.println("method 9"); } } class Main { public static void main(String[] args) { ImplementedClass obj = new ImplementedClass(); obj.methodA(); obj.methodC(); obj.methodF(); } }
The output of the above code after compilation is: