The text given in this section borrows some explanations and code examples from the -- Java Tutorial.
In Java, an interface is a reference type, similar to a class, mainly containing method signatures. Defining an interface is similar to creating a new class except it uses the keyword interface in place of class.
Here is an interface named DrivableVehicle that defines methods needed to drive a vehicle.
public interface DrivableVehicle {
void turn(Direction direction);
void changeLanes(Direction direction);
void signalTurn(Direction direction, boolean signalOn);
}
Note that the method signatures have no braces ({ }) and are terminated with a semicolon.
Interfaces cannot be instantiated—they can only be implemented by classes. When an instantiable class implements an interface, indicated by the keyword implements, it provides a method body for each of the methods declared in the interface.
Here is how a class CarModelX can implement the DrivableVehicle interface.
public class CarModelX implements DrivableVehicle {
@Override
public void turn(Direction direction) {
}
}
An interface can be used as a type e.g., DrivableVechile dv = new CarModelX();.
Interfaces can inherit from other interfaces using the extends keyword, similar to a class inheriting another.
Here is an interface named SelfDrivableVehicle that inherits the DrivableVehicle interface.
public interface SelfDrivableVehicle extends DrivableVehicle {
void goToAutoPilotMode();
}
Note that the method signatures have no braces and are terminated with a semicolon.
Furthermore, Java allows multiple inheritance among interfaces. A Java interface can inherit multiple other interfaces. A Java class can implement multiple interfaces (and inherit from one class).
The design below is allowed by Java. In case you are not familiar with UML notation used: solid lines indicate normal inheritance; dashed lines indicate interface inheritance; the triangle points to the parent.
Staff interface inherits (note the solid lines) the interfaces TaxPayer and Citizen.
TA class implements both Student interface and the Staff interface.
- Because of point 1 above,
TA class has to implement all methods in the interfaces TaxPayer and Citizen.
- Because of points 1,2,3, a
TA is a Staff, is a TaxPayer and is a Citizen.
Interfaces can also contain constants and static methods.
C++ to Java → Miscellaneous Topics →
Java does not directly support constants. The convention is to use a static final variable where a constant is needed. The static modifier causes the variable to be available without instantiating an object. The final modifier causes the variable to be unchangeable. Java constants are normally declared in ALL CAPS separated by underscores.
Here is an example of a constant named MAX_BALANCE which can be accessed as Account.MAX_BALANCE.
public class Account{
public static final double MAX_BALANCE = 1000000.0;
}
Math.PI is an example constant that comes with Java.
This example adds a constant MAX_SPEED and a static method isSpeedAllowed to the interface DrivableVehicle.
public interface DrivableVehicle {
int MAX_SPEED = 150;
static boolean isSpeedAllowed(int speed){
return speed <= MAX_SPEED;
}
void turn(Direction direction);
void changeLanes(Direction direction);
void signalTurn(Direction direction, boolean signalOn);
}
Interfaces can contain default method implementations and nested types. They are not covered here.