Prev Next

Interface

An interface in Java is an abstract type that is used to specify a behaviour that classes must implement. They are similar to protocols. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. For instance, a triangle and a circle covers an area; however, the way you compute their area are pretty different. There is no parent-child or container-contained relationship between them. i.e. one is not the extension of the other nor contained within the other. However, they both are related in that they have to implement 'area' method. Interface is used to capture the relationship between them. In such a case, you define an interface "Shape" and both Triangle and Circle implement the Shape interface.

Defining Interface

We define an interface for Shape, very similar to class but methods are not implemented.

  // Shape.java
  public interface Shape {

    public static final double pi = 3.14; 
    
    public double area();
    public double perimeter();
  }

A number of points to note

  • An interface needs to be defined in a file bearing the same name (like class).

  • Only the method signature is specified and not its body.

  • An interface cannot be instantiated like a class. For example,
    Shape s = new Shape(); is not possible

  • There are no constructors for an interface, since it cannot be instantiated.

  • You can't define instance attributes for the same reason.

  • You can define only static final attributes.

    public static final double pi = 3.14;

  • final -> pi value cannot be changed during its lifetime
  • static -> pi value does not vary with objects of type Triangle, Circle, etc.
  • public -> pi can be accessed directly by "Shape.pi". We make it public since it is a kind of global constant.

Note that it doesn't make sense to declare methods for getters or setters in an interface, since no object attributes are defined. i.e. only 'static final' attributes are defined in an interface for which values have to be set right there.

Implementing an Interface

Typically, a class implements an interface. For example, now Circle class can be implemented as follows.

Note: You will be forced to implement both the area() and perimeter() methods. Try not implementing at least one of them and check out the what happens.

  public class Circle implements Shape {
      protected double radius;

     // @override
     public double area() {
          return pi * radius * radius;
     }

     // @override
     public double perimeter() {
          // Your code
     }
}

Annotate methods with @Override. It allows the Java compiler to catch errors where you think you are overriding (or implementing) a method, but are actually defining a new method (different signature). Note: @override is commented. Either // @override or /* @override */

In a similar fashion define classes for Triangle, Square and Rectangle.

Write driver classes to invoke, work out and understand.

Extending Interface to an Interface

You can extend an interface to make a bigger interface. For example, if X is an interface, interface Y can be extended from X as follows.

  public interface X {

      public void a();
      public void b();
  }

  public interface Y extends X {

      public void c();
  }

Now any class that implements Y must necessarily implement a(), b() and c().

But a class that implements X needs to implement only a() and b().

Implementing Multiple Interfaces

You can implement multiple interfaces simultaneously unlike extending multiple classes. Lets say you have two intefaces X and Y. A class Z can implement both X and Y simultaneously.

  public interface X {
       public void a();
       public void b();
  }

  public interface Y {
       public void c();
       public void d();
  }

  public class Z implements X, Y {
       public void a() { // code here };
       public void b() { // code here };
       public void c() { // code here };
       public void d() { // code here };      
  }

You have to implement all the methods that belong to both these interfaces.