9.9 Interfaces

In the previous section you learned that an abstract class has both methods with bodies and methods with no bodies (abstract methods). You also learned that abstract methods must be overridden in a subclass.

An interface is similar to an abstract class that has no fields and all abstract methods. Interfaces cannot be instantiated—they can only be implemented by classes. The purpose of an interface is to specify behavior for a class.

In other words, we can say that an interface is a design contract. It specifies methods, and classes can "implement" the interface, and thereby sign the contract.

Defining an interface is similar to creating a new class:

public interface Animal
{
//Methods without implementation. String getName(); String makeNoise();
}

To use an interface, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. For example,

public class Dog implements Animal
{
   private String name; // To hold name of dog.

   //Constructor that accept name as parameter.
   public Dog(String name)
   {
      this.name = name;
   }
   
   // method required to implement the animal interface.
   public String getName()
   {
      return name;
   }
   
   // method required to implement the animal interface.
   public String makeNoise()
   {
      return "woof! woof!";
   }
}
public class Cat implements Animal
{
   private String name; // To hold name of cat.

   //Constructor that accept name as parameter.
   public Cat(String name)
   {
      this.name = name;
   }
 
   // method required to implement the animal interface.
   public String getName()
   {
      return name;
   }

   // method required to implement the animal interface. 
   public String makeNoise()
   {
      return "Meow!";
   }
}

An interface reference variable can reference any object that implements that interface, regardless of its class type. This is shown in following example :


import java.util.ArrayList;

public class Zoo
{
    public static void main(String[] args)
    {
        // create an ArrayList of animals
        ArrayList<Animal> animals = new ArrayList<Animal>();
        animals.add(new Dog("Krypto")); // append a Dog object to the list
        animals.add(new Cat("Bella")); // append a Cat object to the list
        animals.add(new Dog("Rocky")); // append a Dog object to the list
        animals.add(new Cat("Molly")); // append a Cat object to the list
        
        // generically process each element in arraylist animals        
        for (Animal animal: animals) 
        {
            System.out.println(animal.getName()+ " : " + animal.makeNoise());
        }
    }
}      

Output :

Krypto : woof! woof!
Bella : Meow!
Rocky : woof! woof!
Molly : Meow!

 

Variables in Interfaces

Variables declared inside interfaces are treated as final and static. You can use interfaces to import shared constants into multiple classes.

public Interface SampleInterface
{
   int NO = 0;
   int YES = 1; 
   void someMethod();
}

Interfaces Can Be Extended

One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.

Java allows a class to implement multiple interfaces. When a class implements multiple interfaces, it must provide the methods specified by all of them.