An interface is a fully abstract class used to specify the behavior of a class. A Java interface may contains static constants, default methods, static methods, nested tyoes and abstract methods (methods without a body). Method bodies exist only for default methods and static methods.
A class implements an interface, thereby inheriting the abstract methods of the interface. A class implementing an interface must define all methods of an interface unless implementing class is abstract.
We use the interface keyword to create an interface in Java.
interface Shape { // Abstract methods. public int getArea(); public int getCurcumference(); }
In above example, we have created an interface named Shape. This interface contains two abstract methods getArea and getCurcumference. Any class implementing Shape interface must define the body of getArea and getCurcumference method.
Here is an example program to implement above mentioned Shape interface.
interface Shape { // Abstract methods. public int getArea(); public int getCircumference(); } class Square implements Shape { int side; Square(int side) { this.side = side; } public int getArea(){ return side * side; } public int getCircumference(){ return 4 * side; } } class Rectangle implements Shape { int length, width; Rectangle(int length, int width) { this.length = length; this.width = width; } public int getArea(){ return length * width; } public int getCircumference(){ return 2 * (length + width); } } public class JavaInterface { public static void main(String[] args) { // Create objects of Square and Rectangle class Square sq = new Square(10); Rectangle rec = new Rectangle(10, 20); // Print area of Shapes System.out.println("Area of Square: " + sq.getArea()); System.out.println("Area of Rectangle: " + rec.getArea()); // Print circumference of Shapes System.out.println("Circumference of Square: " + sq.getCircumference()); System.out.println("Circumference of Rectangle: " + rec.getCircumference()); } }Output
Area of Square: 100 Area of Rectangle: 200 Circumference of Square: 40 Circumference of Rectangle: 60
In above program, class Square and Rectangle implements Shape interface. The formulae for calculating area and circumference is different for Square and Rectangle. Hence, both these classes contains specific implementation of interface methods.
Why do we need an Interface
- Similar to abstract classes, interfaces help us to achieve abstraction in Java. getArea() method calculates the area of a shape but the way area is calculated is different for different shapes. Hence, the implementation of getArea() is different for different shapes like square, rectangle etc.
- An Interfaces provide the blueprint of the implementing class. Any class implementing the interface must follow the blueprint of the interface. For example, any class implementing Shape interface must provide two public methods which returns area and circumference of a shape.
- Interfaces are also used to achieve multiple inheritance in Java. Since multiple inheritance is not allowed in Java, a class can implement multiple interfaces to achieve multiple inheritance.
- A class can implement an interface by using "implements" keyword.
- An interface can contain any number of methods.
- An interface cannot contain a constructor.
- We cannot create an object of interface directly.
- An interface can be empty, with no variables and methods definition.
- An interface can extend multiple interfaces.
- We cannot use final keyword in the interface definition, as it will result in a compiler error.
- All interface declarations must have public or default access modifier. The compiler will automatically add abstract modifier.
- An interface method cannot be final or protected.
- Interface methods do not have a body, the body is provided by the implementing class.
- Interface variables are public, static, and final by default. We cannot change their visibility.
Implementing Multiple Interfaces
A class can also implement multiple interfaces in Java. Interfaces are also used to achieve multiple inheritance in Java. Since multiple inheritance is not allowed in Java, a class can implement multiple interfaces to achieve multiple inheritance. To implement multiple interfaces, keyword implements is used once followed by a list of comma-separated interfaces. For Example:
class A implements B, C { // Code }
Java program to implement multiple interfaces
interface Area { public int getArea(); } interface Circumference { public int getCircumference(); } class Square implements Area, Circumference { int side; Square(int side) { this.side = side; } public int getArea(){ return side * side; } public int getCircumference(){ return 4 * side; } } public class MultipleInterfaces { public static void main(String[] args) { Square sq = new Square(10); // Print area and circumference System.out.println("Area of Square: " + sq.getArea()); System.out.println("Area of Square: " + sq.getCircumference()); } }Output
Area of Square: 100 Area of Square: 40
In above program, class square is implementing both Area and Circumference imterface. In this case, Square class must implement all abstract methods of both interfaces.
Extending an Interface
An interface can extend another interface in the same way that a class can extend another class in java. The extends keyword is used to extend an interface, and the child interface inherits all methods of the parent interface. For Example:
interface A extends B { // members of A }
A class implementing child interface must implement all abstract methods of child as well as parent interface.
Java program to extend an interfaces
interface Eat { public void eat(); } interface Digest { public void digestFood(); } class Chicken implements Digest { public void eat() { System.out.println("Chicken is eating"); } public void digestFood() { System.out.println("Chicken is digesting food"); } } public class ExtendInterfaces { public static void main(String[] args) { Chicken chicken = new Chicken(); chicken.eat(); chicken.digestFood(); } }Output
Chicken is eating Chicken is digesting food
In above program, interface Digest extends interface Eat and class Chicken is implementing Digest interface. In this case, Chicken class must implement abstract methods of both Eat and Digest interfaces.