Introduction
This article contains the exact description of interface in Java along with some easy and understandable illustrations with diagrams for practical aspects.
Interface
The interface is a keyword used in Java and is declared as an interface. Just like with classes, an interface can also be declared as public.
An interface is a mechanism to obtain the abstraction and multiple inheritances in Java. There can be only abstract methods in the interface without any implementation. Interfaces cannot be instantiated on their own, so must be implemented by a class or extended by another interface to be used. It also has a “is a relationship”.
- Java compiler adds public and abstract keywords before the interface methods.
- Static, final and public keywords before data members.
In other words, methods are public and abstract and interface data members are public, static and final.
Why to use interface in Java?
Basically an interface is a very important keyword in Java and it has the main advantages that are listed below:
- The interface helps to obtain full abstraction in Java.
- The interface helps to do loose coupling in Java application.
- The interface helps to obtain multiple inheritances in Java.
Class-Interface relationship
An interface has an abstract method in it and cannot be instantiated. To make an interface useful one should do the following:
- Interface itself must be implemented by a non-abstract class.
- A child interface in the last hierarchy must be implemented by a non-abstract class.
- If an abstract class implements the interface then the abstract class itself must be extended by a non-abstract class.
A class can extend another class, an interface can extend another interface and a class can implement an interface.
A simple illustration of an interface
In this example, the Day interface has only one method and is implented in the class Night.
- interface Day {
- void show();
- }
- class Night implements Day {
- public void show() {
- System.out.println("Sun brights in day time");
- }
- public static void main(String args[]) {
- Night n = new Night();
- n.show();
- }
- }
Output
Sun brights in daytime
The following shows how multiple inheritances can be done with an interface:
- interface Apple {
- void show();
- }
- interface Rose {
- void display();
- }
- class Category implements Apple, Rose {
- public void show() {
- System.out.println("Apple is a fruit");
- }
- public void display() {
- System.out.println("Rose is a flower");
- }
- public static void main(String args[]) {
- Category n = new Category();
- n.show();
- n.display();
- }
- }
Output
Apple is a fruit.
Rose is a flower.
Another example of an interface
In this example, there are two same methods in two different interfaces. While implementing this as a class then there will be ambiguity, but in the case of an interface, it will work properly without any ambiguity. Let's see what happens.
- interface Apple {
- void display();
- }
- interface Pineapple {
- void display();
- }
- class Category implements Apple, Pineapple {
- public void display() {
- System.out.println("Apple and Pineapple are fruits");
- }
- public static void main(String args[]) {
- Category n = new Category();
- n.display();
- }
- }
Output
Apple and Pineapple are fruits.
The interface extends interface or nested interface
An interface can extend any number of other interfaces.
- interface Maths {
- void display();
- }
- interface Geometry extends Maths {
- void show();
- }
- interface Trigonometry extends Geometry {
- void print();
- }
- class Science implements Trigonometry {
- public void display() {
- System.out.println("Math is a subject");
- }
- public void show() {
- System.out.println("Geometry is a sub-part of maths");
- }
- public void print() {
- System.out.println("Trigonometry is a sub-part of maths");
- }
- public static void main(String args[]) {
- Science G = new Science();
- G.display();
- G.show();
- G.show();
- }
- }
Output
Math is a subject
Geometry is a sub-part of maths
Trigonometry is a sub-part of maths
Interface inside a class
It is possible to declare an interface inside a class, in this case, the nested interface can be accessed by the outer class name and are declared public static by default.
- public class Animal {
- interface Dog {
- public void bark();
- }
- }
- class Name implements Animal.Dog {
- public void bark() {
- System.out.println("Barking dog seldom bite");
- }
- public static void main(String args[]) {
- Name N = new Name();
- N.bark();
- }
- }
Output
A barking dog seldom bites
A class inside an interface
- public interface Animal {
- public class Flower {
- interface Dog {
- public void bark();
- }
- }
- }
- class Name implements Animal.Flower.Dog {
- public void bark() {
- System.out.println("Barking dog seldom bite and rose is a flower");
- }
- public static void main(String args[]) {
- Name N = new Name();
- N.bark();
- }
- }
Output
A barking dog seldom bite and rose is a flower
Marker or tagged interface in Java
An interface that has no data members or method in it is known as a marker or tagged interface such as cloneable, Serializable, Remote and so on.
They are just used as a marker that tells JVM to perform some useful and important tasks.
How this interface is written:
- public interface Cloneable {}