Interface
In this section, we study interface.
Interface
An interface is a special class. It is similar to an abstract class.It contains a set of public abstract methods.
***Note: Beginning in Java 8, interface may have default methods which can have a body.
It cannot be instantiated.
Methods listed in an interface must be written elsewhere.
The purpose of an interface is to specify behavior for other classes.
It is often said that an interface is like a “contract”, and when a class implements an interface it must fulfill the contract by implementing the methods listed in the interface.
An interface looks similar to a class, but the keyword interface is used instead of the keyword class.
The methods that are specified in an interface have no bodies, only headers that are terminated by semicolons.
public interface ThreeDimensionalShape {
//abstract methods
public double calculateArea();
public double calculateVolume();
}
public double calculateArea();
public double calculateVolume();
How to Use Interface
To use an interface, a class must specify that it implements the interface and must implement each method in the interface with the signature specified in this interface.If the class does not implement the methods specified in the interface, it must be declared as abstract class.
Implementing an interface is like signing a contract with the compiler – “I will implement all the methods specified by the interface.”
public class Cube implements ThreeDimensionalShape {
private double base;
//constructors, setters, and getters
public double calculateArea() {
return base * base * 6;
}
public double calculateVolume() {
return base * base * base;
}
}
//constructors, setters, and getters
public double calculateArea() {
return base * base * 6;
}
public double calculateVolume() {
return base * base * base;
}
Java does not allow subclasses to inherit from more than superclass, but it allows a subclass to implement multiple interfaces.
An interface can contain field declarations and all the fields in an interface are treated as final and static.
Because they automatically become final, you must provide an initialization value.
Polymorphism with Interfaces
Java allows you to create reference variables of an interface type.
ThreeDimensionalShape s1;
An interface reference variable can reference any object that implements that interface, regardless of its class type.
ThreeDimensionalShape s1 = new Cube( 23.5 );
A reference to an interface can point to any class that implements that interface.
But you cannot create an instance of an interface.
//Error:
ThreeDimensionalShape s1 = new ThreeDimensionalShape();
ThreeDimensionalShape s1 = new ThreeDimensionalShape();
When an interface variable refers to an object, only the methods declared in the interface are available,
and explicit type casting is required to access the other methods of an object referenced by an interface reference.
***Note: Beginning in Java 8, interfaces may have default methods.
A default method is an interface method that has a body.