Interfaces and Abstract Classes in Java

This article compares and contrasts interface, abstract class, and concrete class in the Java programming language.

Objectives

Prerequisites

Definitions

abstract method
A method without a body (only a header followed by a semicolon)
concrete method
A normal method; a method with a body
interface
A class-like entity that contains only class (static) attributes and abstract methods. Used as a parent in a realizes hierarchy and as a type for reference variables.
abstract class
A class that cannot be used to create objects (cannot be instantiated) and may contain abstract methods. Used as a parent in an inheritance hierarchy and as a type for reference variables.
concrete class
A normal class. Used for creating objects.

Rules

Example

«interface»
Drawable
+ SPARKLE_COEFF : double
+ draw(g : Graphics2D) : void
UML Class Diagram Key
Visibility
    − private
    ~ (default)
    # protected
    + public
 
Relationships
    ◁─── inheritance (extends)
    ◁------- realizes (implements)
    ◇─── aggregation
    ◆─── composition
    ᐸ------- dependency
    ──── association
 
Others
    package.Class
    italics – abstract
    underline – static
    bold – final class
    ALL CAPS – final variable
┌----------- -┴- ------------┐
ChessBoard
 
+ draw(g : Graphics2D) : void
ChessPiece
# color : String
+ move(location : int) : boolean
|
King
− moved : boolean
+ draw(g : Graphics2D): void
+ move(location : int): boolean
|
────────────┤
|
|
|
Queen
 
+ draw(g : Graphics2D): void
+ move(location : int): boolean
|
────────────┤
|
|
|
Pawn
− firstMove : boolean
+ draw(g : Graphics2D): void
+ move(location : int): boolean
|
────────────┘
import java.awt.Graphics2D;


interface Drawable {

    // class attribute because it is static
    public static final double SPARKLE_COEFFICIENT = 0.26;

    // abstract method because it has no body
    public void draw(Graphics2D g);
}


// concrete class because it isn't abstract
class ChessBoard implements Drawable {

    // concrete method because it has a body
    public void draw(Graphics2D g) {
        ⋮
    }
}


// abstract class
abstract class ChessPiece implements Drawable {

    // instance attribute because it isn't static
    protected String color;

    // abstract method because is has no body
    public abstract boolean move(int location);
}


// concrete class because it isn't abstract
class King extends ChessPiece {

    // instance attribute because it isn't static
    private boolean moved = false;

    // concrete method because it has a body
    public boolean move(int location) {
        ⋮
        moved = true;
        return …
    }

    // concrete method because it has a body
    public void draw(Graphics2D g) {
        ⋮
    }
}


// concrete class because it isn't abstract
class Queen extends ChessPiece {

    // concrete method because it has a body
    public boolean move(int location) {
        ⋮
        return …
    }

    // concrete method because it has a body
    public void draw(Graphics2D g) {
        ⋮
    }
}


// concrete class because it isn't abstract
class Pawn extends ChessPiece {

    // concrete method because it has a body
    public boolean move(int location) {
        ⋮
        return …
    }

    // concrete method because it has a body
    public void draw(Graphics2D g) {
        ⋮
    }
}