© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_25

25. Interface – Total Abstraction

Ron Dai1 
(1)
Seattle, WA, USA
 

The concept of interface is a part of abstraction, one of the four OOP. characteristics. Abstraction is about an abstract design of common features, including operations of the object.

Interface is the blueprint of a class. However, it is neither a class nor an object. All methods defined in an interface are abstract. There are no implementation details allowed inside any method of the interface. The class that is going to implement the interface will take care of the actual implementation of the methods.

Let’s look at an example of an interface and classes implemented from it. Auto is a general term representing vehicles. We use Auto to define an interface.
public interface Auto {
       void start();
       void stop();
       void turn();
       void back();
       void park();
}

As two common types of automobiles, cars and buses are common objects. Both cars and buses share the same type of behaviors defined in Auto interface. When we create a class for cars and buses, we use the keyword implements to implement car and bus from the same Auto interface with different behavioral details.

We use class Car as an example:
public class Car implements Auto {
       private String maker;
       public void start() {
              // car starts its engine
       }
       public void stop() {
              // car stops its engine
       }
       public void turn() {
              // car turns left or right at a corner
       }
       public void back() {
              // car backs
       }
       public void park() {
              // car parks
       }
       public String getMaker() {
              return this.maker;
       }
       public void setMaker(String maker) {
              this.maker = maker;
       }
}
As you probably have noticed,
  • An interface indicates what the object can do.

  • When a class implements the interface, it defines what the object is doing with the necessary details.

The design of the interface allows developers to modify the underlying classes without altering the callers’ implementations; this is sometimes called coding to the interface. There are at least two circumstances when we should consider adopting an interface design.
  • When we want to only specify the behavior of a particular data type, without being concerned about whoever implements its behavior.

    For example:

    We define an interface Auto that is an abstract concept and a general term. In this interface, we define several methods such as start, stop, turn, back, and park by their signatures, without any implementation details. We will add implement details in these methods when we create Car or Truck classes that implement the Auto interface.

  • When all classes have the same structure, but they totally have different functionalities.

    For example:

    Dogs and cats communicate in totally different ways. A dog barks, but a cat meows. We may define an interface Animal and create a class Dog and class Cat, like shown here.
    public interface Animal {
           public void communicate();
    }
    public class Dog implements Animal {
           public void communicate() {
                  System.out.println("bark, bark!");
           }
    }
    public class Cat implements Animal {
           public void communicate() {
                  System.out.println("meow, meow...");
           }
    }
Java supports multiple interface implementation: for example, if we define another interface, MovingObject as shown.
public interface MovingObject {
       void movingNorth();
       void movingSouth();
}
Class Car can implement from both interfaces, Auto and MovingObject as shown here.
public class Car implements Auto, MovingObject {
       ...
       public void movingNorth() {
              // car moves North
       }
       public void movingSouth() {
              // car moves South
       }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.101.60