Interfaces

0

Interfaces

The public interface of a class is a contract between the client code and the class that provides the service. Concrete classes implement each method. However, an abstract class can defer the implementation by declaring the method to be abstract, and a Java interface declares only the contract and no implementation.

A concrete class implements an interface by defining all methods declared by the interface. Many classes can implement the same interface. These classes do not need to share the same class hierarchy. Also, a class can implement more than one interface. This is described in the following sections.

As with abstract classes, use an interface name as a type of reference variable. The usual dynamic binding takes effect. References are cast to and from interface types, and you use the instanceof operator to determine if an object’s class implements an interface.




Note – All methods declared in an interface are public and abstract, no matter if we explicitly mention these modifiers in the code or not. Similarly, all attributes are public, static and final; in other words, you can only declare constant attributes.

The Flyer Example

Imagine a group of objects that all share the same ability: they fly. You can construct a public interface, called Flyer, that supports three operations: takeOff, land, and fly. (see Figure 1).

fig1
1 public interface Flyer {

2 public void takeOff();

3 public void land();

4 public void fly();

5 }

1 public class Airplane implements Flyer {

2 public void takeOff() {

3 // accelerate until lift-off

4 // raise landing gear

5 }

6 public void land() {

7 // lower landing gear

8 // decelerate and lower flaps until touch-down

9 // apply brakes

10 }

11 public void fly() {

12 // keep those engines running

13 }

14 }

There can be multiple classes that implement the Flyer interface, as shown in Figure 2. An airplane can fly, a bird can also fly, Superman can fly, and so on.

fig2

An Airplane is a Vehicle, and it can fly. A Bird is an Animal, and it can fly. These examples show that a class can inherit from one class but also implement some other interface.

This sounds like multiple inheritance, but it is not quite that. The danger The following describes the Bird class:

public class Bird extends Animal implements Flyer {

public void takeOff() { /* take-off implementation */ }

public void land() { /* landing implementation */ }

public void fly() { /* fly implementation */ }

public void buildNest(){ /* nest building behavior */ }

public void layEggs() { /* egg laying behavior */ }

public void eat() { /* override eating behavior */ }

}

The extends clause must come before the implements clause. The Bird class can supply its own methods (buildNest and layEggs), as well as override the Animal class methods (eat).of multiple inheritance is that a class could inherit two distinct implementations of the same method. This is not possible with interfaces because an interface method declaration supplies no implementation, as shown in Figure 3.





Suppose that you are constructing an aircraft control software system. It must grant permission to land and take off for flying objects of all types. This is shown in Figure 4.

fig4

The code for the airport could look like the following:

1 public class Airport {

2 public static void main(String[] args) {

3 Airport metropolisAirport = new Airport();

4 Helicopter copter = new Helicopter();

5 SeaPlane sPlane = new SeaPlane();

6

7 metropolisAirport.givePermissionToLand(copter);

8 metropolisAirport.givePermissionToLand(sPlane);

9 }

10

11 private void givePermissionToLand(Flyer f) {

12 f.land();

13 }

14 }

Multiple Interface Example

A class can implement more than one interface. Not only can the SeaPlane fly, but it can also sail. The SeaPlane class extends the Airplane class, so it inherits that implementation of the Flyer interface. The SeaPlane class also implements the Sailer interface. This is shown in Figure 5.

fig5

Now write the Harbor class to give docking permission:

1 public class Harbor {

2 public static void main(String[] args) {

3 Harbor bostonHarbor = new Harbor();

4 RiverBarge barge = new RiverBarge();

5 SeaPlane sPlane = new SeaPlane();

6

7 bostonHarbor.givePermissionToDock(barge);

8 bostonHarbor.givePermissionToDock(sPlane);

9 }

10 private void givePermissionToDock(Sailer s) {

11 s.dock();

12 }

13 }

The seaplane can take off from Metropolis airport and dock in Boston harbor.

Uses of Interfaces

You use interfaces to:

● Declare methods that one or more classes are expected to implement

● Reveal an object’s programming interface without revealing the actual body of the class (this can be useful when shipping a package of classes to other developers)

● Capture similarities between unrelated classes without forcing a class relationship

● Simulate multiple inheritance by declaring a class that implements several interfaces