Architecture – Abstract class vs Interface

Abstract class and Interface

Abstract class

  • Abstract classes cannot be instantiated, but they can be subclassed
  • An abstract method is a method that is declared without an implementation
  • If a class includes abstract methods, then the class itself must be declared abstract
  • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Comparison

Same

  • Cannot be instantiated
  • Contain a mix of methods declared with or without an implementation

Differences

  • Abstract classes:
    • You can declare fields that are not static and final, and define public, protected, and private concrete methods
    • You can extend only one class, whether or not it is abstract
  • Interfaces:
    • All fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public
    • You can extend only one class, whether or not it is abstract,

When do we use abstract classes or interfaces?

Use abstract classes

  • You want to share code among several closely related classes.
  • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

Use interfaces

  • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
  • You want to take advantage of multiple inheritance of type.

Example of using abstract class

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects.

These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. 

// First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:

abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
    abstract void resize();
}

// Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}
class Rectangle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*