Introduction to Interfaces Tutorial

Prior to Java 8, the purpose of an interface was much easier to explain. A simple statement would just about sum it up – an interface is essentially a 100% abstract class containing only abstract methods and constants. In Java 8, the interface underwent a major overhaul; functionality such as default methods and static methods were introduced. Along with the new functionality, multiple inheritance issues arose. As a beginner, all of this may sound a little scary, but I am going to explain the details of interfaces in great depth. However, this initial tutorial will serve as more of an introduction to interfaces as opposed to explanation of all the rules.
It is important to understand the IS-A relationship that a subclass has with an abstract superclass before learning about interfaces. For example, a Boeing 787, FA18 Fighter, and the Cessna Amphibian all have an IS-A relationship with an Airplane. A Formula 1, Honda Accord, and a Toyota Camry all have an IS-A relationship with a Car. Are there some things that a Honda Accord has in common with a Boeing 787? They both have engines and they both carry passengers. Think of the purpose of an interface as having more of an inheritance type CONTAINS-A-AN relationship with an object, whereas an abstract class has more of an inheritance IS-A-AN type relationship with an object.

Boeing 787           is an           Airplane             Airplane - abstract class
FA18 Fighter         is an           Airplane             Airplane - abstract class
Cessna Amphibian     is an           Airplane             Airplane - abstract class
Formula 1            is a            Car                  CAR - abstract class
Honda Accord         is a            Car                  CAR - abstract class
Toyota Camry         is a            Car                  CAR - abstract class
Boeing 787           contains an     Engine               Engine - interface
FA18 Fighter         contains an     Engine               Engine - interface
Cessna Amphibian     contains an     Engine               Engine - interface
Formula 1            contains an     Engine               Engine - interface
Honda Accord         contains an     Engine               Engine - interface
Toyota Camry         contains an     Engine               Engine - interface
Boeing 787           contains        Passengers           Passengers - interface
FA18 Fighter         contains        Passengers           Passengers - interface
Cessna Amphibian     contains        Passengers           Passengers - interface
Formula 1            contains        Passenger            Passengers - interface
Honda Accord         contains        Passengers           Passengers - interface
Toyota Camry         contains        Passengers           Passengers - interface



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md InterfaceIntro
C:\Java>cd InterfaceIntro
C:\Java\InterfaceIntro>Notepad InterfaceIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class InterfaceIntro {
    public static void main(String args[]) {
        Boeing787 commercial = new Boeing787("787", 590,"jet fuel");
        commercial.summary(); 
        commercial.takeOff();
        System.out.println("The engines of a 787 burn " + commercial.getFuelType());
        System.out.println("A 787 can safely carry up to " + commercial.getMaxPassengers() + " passengers");

        System.out.println();
        HondaAccord accord = new HondaAccord("Accord", "sedan", "un-leaded gasoline");
        accord.summary();
        accord.fastenSeatBelts();
        System.out.println("The engine of a Honda Accord burns " + accord.getFuelType());
        System.out.println("The Honda Accord is designed to safely carry " + accord.getMaxPassengers() + " passengers");

    }
}

abstract class Airplane {
    private String model = "";
    private int topSpeed = 0;

    Airplane() { super(); }
    Airplane(String model, int topSpeed) { 
        this.model = model;
        this.topSpeed = topSpeed;
    }

    void summary() { System.out.println("The top speed of a " + model + " is: " + topSpeed); }

    abstract void takeOff(); // abstract method - no code block/body
}

abstract class Car {
    private String model = "";
    private String carType = "";

    Car() { super(); }    
    Car(String model, String carType) {
        this.model = model;
        this.carType = carType;
    }

    void summary() { System.out.println("The " + model + " is a " + carType  ); }

    abstract void fastenSeatBelts();
}

interface Engine {
    String getFuelType();
}

interface Passengers {
    int getMaxPassengers();    
}

class Boeing787 extends Airplane implements Engine, Passengers {
    private String fuelType = "";
    static final int MAX_PASSENGERS = 335;

    Boeing787() { super(); }
    Boeing787(String model, int topSpeed, String fuelType) {
        super(model, topSpeed);
        this.fuelType = fuelType;
    }
    @Override
    void takeOff() {
        System.out.println("The 787 needs a very long runway to takeoff ...");
    }

    @Override
    public String getFuelType() {
        return fuelType;
    }
    @Override
    public int getMaxPassengers() {
        return MAX_PASSENGERS;
    }

} 

class HondaAccord extends Car implements Engine, Passengers {
    private String fuelType = "";
    static final int MAX_PASSENGERS = 5;

    HondaAccord() { super(); }
    HondaAccord(String model, String carType, String fuelType) {
        super(model, carType);
        this.fuelType = fuelType;
    }

    @Override
    void fastenSeatBelts() {
        System.out.println("Safety first! Pull the seatbelt over your shoulder and lap and click it together.");
    }

    @Override
    public String getFuelType() {
        return fuelType;
    }
    @Override
    public int getMaxPassengers() {
        return MAX_PASSENGERS;
    }
}


Now switch back to the command prompt (CMD) and type in javac InterfaceIntro.java and press Enter.
Now type in java InterfaceIntro and press Enter.


C:\Java\InterfaceIntro>javac InterfaceIntro.java
C:\Java\InterfaceIntro>java InterfaceIntro
The top speed of a 787 is: 590
The 787 needs a very long runway to takeoff ...
The engines of a 787 burn jet fuel
A 787 can safely carry up to 335 passengers

The Accord is a sedan
Safey first! Pull the seatbelt over your shoulder and lap and click it together.
The engine of a Honda Accord burns un-leaded gasoline
The Honda Accord is designed to safely carry 5 passengers


Final thoughts

Basically this tutorial was an introduction to demonstrate a simple use case of an interface. In my following tutorials, I will cover in detail the rules and capabilities of interfaces.


Tutorials