Skip to content

Class VS Abstract Class VS Interface

tzelynn edited this page Oct 11, 2021 · 2 revisions

Summary on Class VS Abstract Class VS Interface

Defining them in Java

interface ThreeDShape {
    int getVolume();
}
// remember to define different classes/interfaces/abstract classes in different .java files
abstract class ColouredRect {
    private final String colour;
    private final int height;
    private final int width;

    protected ColouredRect(String colour, int height, int width) {
        this.colour = colour;
        this.height = height;
        this.width = width;
    }

    int getArea() {
        return this.height * this.width;
    }

    abstract boolean floatable();

    @Override
    public String toString() {
        return String.format("%d by %d %s shape", this.height,
                                this.width, this.colour);
    }
}
class ColouredCuboid extends ColouredRect implements ThreeDShape {
    private final String name;
    private final int depth;
    private static final int DENSITY_OF_WATER = 1;

    ColouredCuboid(String colour, int height, int width, int depth, String name) {
        super(colour, height, width);
        this.name = name;
        this.depth = depth;
    }

    @Override
    public int getVolume() {
        return this.getArea() * this.depth;
    }

    @Override
    boolean floatable() {
        return this.getArea() / this.getVolume() < DENSITY_OF_WATER;
    }

    @Override
    public String toString() {
        String output = String.format("%s is a ", this.name);
        output += super.toString();
        output += String.format(" with depth %s", this.depth);
        return output;
    }
}
class ColouredSquare extends ColouredRect {
    ColouredSquare(String colour, int height, int width) {
        super(colour, height, width);
    }

    @Override
    boolean floatable() {
        return true;
    }

    boolean isUnitSquare() {
        return this.getArea() == 1;
    }
}

Important points to note

  • Interface cannot implement any functions within its definition (all functions are abstract) but abstract class can have both implemented and abstract functions, while concrete class cannot have abstract functions (must implement all functions)
    • see getVolume() VS getArea() and floatable() above
  • In a concrete class that extends an abstract class and implements an interface, functions from interface must be public but functions from abstract class do not need to be public
    • see getVolume() VS floatable() above
  • In a concrete class that extends an abstract class and implements an interface, all abstract methods must be implemented in concrete class
    • see getVolume() in ColouredCuboid and floatable() in ColouredCuboid and ColouredSquare
  • Concrete class can use functions from abstract parent class without overriding if they have already been implemented in parent class
    • see getArea() above
  • Concrete class can implement its own functions not from the parent class it extends or interface it implements
    • see isUnitSquare() above
Clone this wiki locally