Skip to content

Abstract Classes and Interfaces

Büşra Oğuzoğlu edited this page Jul 18, 2022 · 2 revisions

Abstract Classes:

Abstract classes can not be instantiated (Instances cannot be created using new keyword). For example, it makes sense to make Shape class abstract, because we want circle or rectangle objects, not shape objects. However, we need the Shape class for inheritence and polymorphism. We want our code to be clean and easily understandable.

getArea() and getPerimeter() methods are needed for both Circle and Rectangle objects. So, they can be defined in the Shape super class for simplicity. However, we cannot implement these methods in the Shape class, they should only be declared in the Shape without their implementations if Shape is an abstract class. Therefore, these methods (methods of abstract class, they are only declared, not implemented) are called abstract methods.

In UML diagrams, abstract class names and abstract methods are italicized.

Example of an abstract class:

public abstract class Shape {
    protected String color;
    protected double area;
    protected double perimeter;
    
    // Constructors of abstract classes should be ‘protected’ to prevent instantiation
    protected Shape() {}
    protected Shape(String color) {
        this.color = color;
    }

    // Abstract classes contain abstract methods without method bodies
    protected abstract double getArea();
    protected abstract double getPerimeter();
}

Methods of the abstract class are overriden in child classes. (Denoted by @Override)

Most of the time, we see abstract super classes and concrete subclasses however, it is possible for the superclass to be concrete and child class to be abstract. (All classes that we define are child classes of Java's Object class, it is concrete, but we can define abstract classes like Shape.)

If a class contains an abstract method, it should be defined as abstract. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined as abstract as well (It inherits abstract methods, therefore it should be abstract if the methods are still abstract). Also, abtsract methods are non-static.

Interfaces:

Interface’s intent is to specify common behavior for objects, they are a collection of method declarations with no bodies (Methods of an interface are always empty; they are simply method signatures without implementations)

Interfaces do not have constructors and they cannot be directly instantiated, they are about actions and behaviors rather than objects.

When a class implements an interface, it must implement all of the methods declared in the interface.

An example interface, sellable: Note that it defines a behavior or action:

public interface Sellable {
    // returns the price
    public int listPrice();
    // returns the lowest possible price
    public int lowestPrice();
    // reduces the price
    public void makeDiscount();
}

A class implements an interface, it should override all the methods that are given in the interface (in that sense, it is similar to an abstract class, but remember that they are different abstract classes still define some kind of object):

public class Product implements Sellable {
    private int price;
    private double discountRate = 0.1;
    
    @Override
    public int listPrice() {
        return price;
    }
    @Override
    public int lowestPrice() {
        return price / 2;
    }
    @Override
    public void makeDiscount() {
        price = (int) (price * discountRate);
    }
}

In Java, a class can implement multiple interfaces (it may only extend one other class). We can remember it by thinking that an object can have multiple behavioral properties.

public class Product implements Sellable, Printable {
}

Interfaces can extend multiple interfaces unlike classes: (Known as multiple inheritence)

public interface A extends B, C {
    public int myMethod();
}

In UML diagrams, interface names are italicized and <> keyword is added in the header.

Clone this wiki locally