Java interface

In this lesson of Java course, we will learn about the java interface. We will take a look at the importance of Java interfaces and see how to implement interfaces in Java with some examples.

1. Java Interface

Java interface is a full abstract class with abstract method with no body or definition. You can think of Java interface as a blueprint that outlines few methods which every class who use this interface must implement. Java interface is similar to a Java class except

  1. All methods in the interface are abstract.
  2. All classes which use these interfaces in Java must implement the method defined in the interface.

Let’s take a look at an example to understand more clearly:

 public interface Vehicle {
  //constants
  public static final DataType CONSTANT_NAME = value;

  //abstract methods
  public void vehicleType();
}
  • Vehicle is an interface.
  • We use interface keyword to create an interface in Java.
  • Above interface define 2 abstract methods as vehicleType() ,any class that implement this interface must provide definition to these methods.
Do you know Java 8 added few enhancement to Java interface which includes default and static method declaration in the interface.

2. Implementing Java Interface

Like the abstract classes, we can’t create object of the interface. Interface should be implemented by a class using implements keyword. Let’s take a look at our car example to understand the Java interfaces in details:

interface Vehicle {
  void vehicleType(final String type);
}

//implement the Vehicle interface
class Audi implements Vehicle{
  
  // implementation of abstract method
  public void vehicleType(final String type){
     System.out.println("It's an Audi:: "+type);
  }
}

class JavaInterfaceExample {
  public static void main(String[] args) {
    Audi q5 = new Audi();
    q5.vehicleType("Q5");
  }
}

Output

It's an Audi:: Q5
  • We created interface Vehicle.
  • Out interface contains an abstract method vehicleType().
  • Audi class implements the interface by using the implements keyword.
  • Our Audi class also provide the implementation for the vehicleType() method.

2.1. Implementing Multiple Interfaces

Java provides the option to use multiple interfaces while using in the Java class. Let’s take the following example

interface Vehicle {
  void vehicleType(final String type);
}

interface Audi {
  void displayAudiDetails();
}


//implement the Vehicle interface
public class AudiQ5 implements Vehicle, Audi{
  
  // implementation of abstract method from Vehicle
  public void vehicleType(final String type){
     System.out.println("It's an Audi:: "+type);
  }

  //implementation of abstract method from Audi 
  public void displayAudiDetails(){
     System.out.println("Welcome to Audi Q5");
  }
}

class JavaInterfaceExample {
  public static void main(String[] args) {
    AudiQ5 q5 = new AudiQ5();
    q5.vehicleType("Q5");
    q5.displayAudiDetails();
  }
}

Output

It's an Audi:: Q5
Welcome to Audi Q5

2.2. Extending Interfaces

In Java, interfaces can extends other interface using the extends keyword.

interface Vehicle {
  void vehicleType(final String type);
}

interface Audi extends Vehicle {
  void displayAudiDetails();
}

2.3 Extending multiple Java interfaces

An interface can extend multiple interfaces. For example,

interface Automobile {
}

interface Vehicle {
  void vehicleType(final String type);
}

interface Audi extends Automobile,Vehicle {
  void displayAudiDetails();
}

3. Java interface Advantages

Interfaces in java provides a lot of advantages and flexibility.

3.1. Abstraction

Java interface provides way to use abstraction in our Java programming. Since interface don’t allow defining methods, it makes easier to hide the actual implementation in the class and only expose the method to the outside world using the interface. We will look in more details once we get in to advance topic which can explain how Java classes can have some additional methods which are not exposed to outside world.

3.2 Multiple Inheritance

Remember we talked about how Java does not allow multiple inheritance with classes. We can still use / achieve multiple inheritance in Java using the interfaces. A class in Java can implements multiple interfaces which allow inheritance from multiple interfaces in Java.

3.3. Polymorphism

Scroll to Top