How to use Interface

Lab Overview

In the previous lab (GLAB - 303.10.3), we demonstrated that an abstract class has both methods with bodies, and methods with no bodies (abstract methods). You learned that abstract methods must be overridden in a subclass.

Learning Objective:

By the end of this lesson, learners will be able to use Java interfaces.

An Interface is similar to an abstract class with 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 that can "implement" the Interface, and thereby sign the contract. We will use the Shapes example in this lab. Suppose that our application involves many shapes that can move. We could define an interface as movable, containing the signatures of the various movement methods.

Begin

Create a class named Shape. This will be an Abstract class and a Super class. Write the code below:

public abstract class Shape {
   protected String color;
   protected double height;  // To hold height.
   protected double width;  //To hold width
   protected double base;  //To  hold base

   public void setColor(String color) {
       this.color = color;
   }

   public void setWidth(double width) {
       this.width = width;
   }

   public void setHeight(double height) {
       this.height = height;
   }

   public void setBase(double base) {
       this.base = base;
   }

   // The getArea method is abstract.
   // It must be overridden in a subclass.
   /** All shapes must provide a method called getArea() */
   public abstract double getArea();

   /** Returns a self-descriptive string */
   public String toString() {
       return "Shape[color=" + color + "]";
   }

   public void displayshapName() {
       System.out.println("I am a Shape.");
   }
}

Create an Interface named Movable. It is similar to creating a new class, as shown below:

Similar to an abstract class, an Interface cannot be instantiated because it is incomplete (the abstract methods' body is missing). To use an interface, you must derive subclasses and provide implementation to all of the abstract methods declared in the interface. The subclasses are now complete and can be instantiated. To derive subclasses from an interface, a new keyboard "implement" is to be used instead of "extends" for deriving subclasses from an ordinary class or an abstract class. It is important to note that the subclass implementing an interface needs to override ALL abstract methods defined in the interface; otherwise, the subclass cannot be compiled.

Create a class named Circle. This will be a Child class. Write the code below. The new constructor will add in the Circle class for coordinates and radius.

Create a class named myRunner. This will be the Main class or entry point for the application. Write the code below.

We can also upcast subclass instances to the Movable interface via Polymorphism, similar to an abstract class.

Practice Task: Create two classes: Rectangle and Triangle. Extend both classes from the Shape class, and give an implementation of the Movable interface.

Last updated