Interfaces are like classes, with one big difference: There is no logic defined in any of the methods (except for one exception, which you'll see shortly). Every method in an interface is an abstract method.
What is an Abstract Method?
The only things you have in an interface are abstract methods. An abstract method is a method that does not have a method body and only defines the method signature. Here is an example of an interface with a few abstract methods:
public interface MotorizedVehicle {
public boolean start();
public void stop();
public void accelerate(int mph);
public void decelerate(int mph);
}
In the MotorizedVehicle interface above, you can see that you have four method signatures. All four are abstract methods: they have no method body.
How to Use an Interface?
Interfaces are like contracts because if you implement one or more (which is often a requirement for a given company/project), you are required to implement the abstract methods in the interface.
The only way to get around this is to declare your implementing class as abstract, which you'll come back to shortly. If you do not declare your class as abstract and you do not implement all abstract methods in the interface, then you will have a compilation error.
The Implements Keyword
Just like the way you use the extends keyword to inherit from a parent class, you use the keyword implements to signify that a class must implement a given interface. One difference between extends and implements is that each class can only extend one other class in Java, but classes can implement as many interfaces as they want.
class Vehicle implements MotorizedVehicle {
// because this class implements the
// MotorizedVehicle Interface you are
// required to implement all four abstract
// methods in the MotorizedVehicle Interface
@Override
public boolean start(){
// write code here to start the vehicle
}
@Override
public void stop() {
// write code here to stop the vehicle
}
@Override
public void accelerate(int mph) {
// write code here to accelerate the vehicle
}
@Override
public void decelerate(int mph) {
// write code here to decelerate the vehicle
}
}
Now that you have the MotorizedVehicle interface, you can make sure that every type of motorized vehicle class contained in your application implements the MotorizedVehicle interface. Because of this, you can be sure that every motorized vehicle is guaranteed to have the four methods outlined in the interface: start(), stop(), accelerate(), and decelerate().
Not only that, but you will know that every type of motorized vehicle class will have those four methods, and they will all share the same method signature. This is very useful because it adds a guarantee that all motorized vehicles will:
- All have those four methods
- All share the same method signature
- All be invoked and integrated identically
For instance, if you have created a Truck class, a Tractor class, a SnowMobile class, a Plane class, or any other type of motorized vehicle, you can rest assured that they will all implement the MotorizedVehicle interface and they will all contain the four methods defined in the MotorizedVehicle interface. One of the reasons this is such a big player in polymorphism (more on this is coming up soon!) is that you can use that interface as an instance variable (known as a dependency) in your classes, and then any class that implements the MotorizedVehicle interface can now be used to satisfy that instance variable/dependency. Take a look at this example:
class PolyMorphExample {
MotorizedVehicle vehicle;
public PolyMorphExample(MotorizedVehicle vehicle){
this.vehicle = vehicle;
}
public void setMotorizedVehicle(MotorizedVehicle vehicle){
this.vehicle = vehicle;
}
public void testVehicle(){
vehicle.start();
vehicle.accelerate();
vehicle.decelerate();
vehicle.stop();
}
}
Now you can use that PolyMorphExample with a number of different vehicle types that all implement the MotorizedVehicle interface and the testVehicle method, and the method calls within it will all act in their own way. For instance:
class Demo {
public static void main(String[] args) {
Plane plane = new Plane();
Motorcycle moto = new Motorcycle();
Tractor tractor = new Tractor();
PolyMorphExample morpho = new PolyMorphExample(plane);
// this will run all the abstract methods
// implemented by the plane class
morpho.testVehicle();
// change the motorized vehicle being
// used to the motorcycle
morpho.setMotorizedVehicle(moto);
// this will run all the abstract methods
// implemented by the Motorcycle class
// (which will act differently than the Plane class
morpho.testVehicle();
// change the motorized vehicle
// being used to the Tractor
morpho.setMotorizedVehicle(tractor);
// this will run all the abstract methods
// implemented by the Tractor class
// (which will act differently than the
// Plane and Motorcycle classes
morpho.testVehicle();
}
}
If this is still a little confusing, don't worry. The most important thing to understand and remember right now is that interfaces define abstract methods. Abstract methods do not have a method body. They only define the method signature. Any class that implements an interface MUST implement the abstract methods defined in the interface they are implementing. Or, they must define the class as an abstract class - which you'll discuss shortly.
Summary: What is an interface Java?
- An
interfaceis a class with no logic - Interfaces only contain abstract methods
- An abstract method is a method with only a signature and no method body
- The
implementskeyword is used when a class must implement an interface
Syntax
Here's the syntax for creating an interface and forcing a class to implement it, where you can substitute the words starting with your_ with your values.
public interface Your_Interface {
// your abstract methods here
}
public class Your_Class implements Your_Interface {
// your logic here
}