Abstract Class and Interface

Abstract Class     Interface

Abstract class can contain fields and methods that implement default behavior, and can also have abstract methods.
Abstract class cannot be instantiated, it can only be subclassed.

Interface can only contain constants (public static and final variables) and method signatures (method without body / abstract method).
Interface cannot be instantiated, it can only be implemented by class or extended by other interface.

Abstract Class
Abstract class are used to declare common characteristics of subclasses, and can contain fields and methods that describe the actions that a class can perform. But abstract class cannot be instantiated, they can only be subclassed.

Why make a class if you can’t make objects out of it?   Because the class might be too.. abstract.
For example, an abstract class Passenger. In real, we might want to make object of passenger with more concrete form, like Adult passenger or Infant passenger. But both adult and infant, are passenger sharing the same default characteristic.

An abstract class can include methods that contain no implementation. These are called abstract methods. The subclass must define an implementation for every abstract method of the abstract superclass.

For example, an abstract class Passenger, having the characteristics of name, gender, and method earningTravelPoint, and abstract method isSeatAvailable.

abstract class Passenger {
    String name;
    String gender;
    Integer travelPoint;
   
    void earningTravelPoint() {
        //increase travel point
    }
   
    // abstract method
    abstract boolean isSeatAvailable(String chosenSeat);
}

Each non-abstract subclass of Passenger, such as AdultPassenger and InfantPassenger will have the same fields of name, gender, travelPoint and able to perform method earningTravelPoint, but also must provide implementation for the abstract method isSeatAvailable:

class AdultPassenger extends Passenger {
    String creditCardNo;
   
    void buyMerchandise() {
        // buy merchandise and charged to credit card
    }
   
    boolean isSeatAvailable(String chosenSeat) {
        //if chosenSeat is not reserved, then return true, else false.
    }
}
 
class InfantPassenger extends Passenger {
    void receiveToysAndMeal() {
        // get toys and baby meal
    }
   
    boolean isSeatAvailable(String chosenSeat) {
        //if chosenSeat is not reserved and not near emergency exit, then return true, else false.
    }
}

 

Interface
An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. Interface defining what a class “can do” without any explanation “how the class will do it“.
Interface usually creating a set of methods without any implementation, and the implementation must be overridden by the implemented class. The advantage is that it provides a flexible way for any class, from any inheritance tree, to have the same common characteristic, and can implement one or more interfaces.

public interface FrozenYoghurt {
    // constant declarations
    public static final double TOTAL_SUGAR = 10.5d;
    public static final double TOTAL_FAT = 0.7d;
   
    // method signatures
    void mixFlavour();
    void addTopping(String topping);
}
 
public interface SnowIce {
    // method signatures
    void shaveIce();
}

Class that implements the interface must provides a method body for each of the methods declared in the interface.

public class GreenMango implements FrozenYoghurt {
    void mixFlavour() {
        // mix two yogurt flavour, consist of original flavour and one other flavour.
    }
   
    void addTopping(String topping) {
        // add topping to frozen yoghurt
    }
}
 
public class ColdDessert implements FrozenYoghurt, SnowIce {
    void mixFlavour() {
        // mix two yogurt flavour, consist of any available flavour.
    }
   
    void addTopping(String topping) {
        // add topping
    }
   
    void shaveIce() {
        // shave ice until become like snow
    }
}

As we can see above, the interface FrozenYoghurt and interface SnowIce define what class can do in general. And then, class GreenMango and ColdDessert can implement those interface and having common methods without any relation one to another, and able to define their own implementation of the method.

 

References :

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.