Interfaces and Abstract Classes in Java
This article compares and contrasts interface, abstract class, and concrete class in the Java programming language.
Objectives
- Distinguish among interface, abstract class, and concrete class
- Know and understand the purpose of an interface and an abstract class
- Know what is allowed in an interface, an abstract class, and a concrete class
Prerequisites
- Understand Java classes, objects, and methods
- Understand Java variable scope (local, instance, and class) and method scope (instance and class)
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
- It is not possible to create an object from an interface or abstract class.
- Interfaces may not contain instance attributes.
- Interfaces may not contain concrete instance methods.
- Concrete classes may not contain abstract instance methods.
- There is no such thing as an abstract class (static) method. It doesn’t make sense.
- Summary
(most abstract) (least abstract) interface abstract class concrete class May be instantiated ✓ May contain class attributes ✓ ✓ ✓ May contain instance attributes ✓ ✓ May contain class methods ✓ ✓ ✓ May contain abstract instance methods ✓ ✓ May contain concrete instance methods ✓ ✓
Example
|
||||||||||||||||||||||||
△ |
|
|||||||||||||||||||||||
┌----------- | -┴- | ------------┐ | ||||||||||||||||||||||
╎ | ╎ | |||||||||||||||||||||||
|
|
|||||||||||||||||||||||
△ | ||||||||||||||||||||||||
| | ||||||||||||||||||||||||
|
|
────────────┤
|
|
|
|||||||||||||||||||||||
| | ||||||||||||||||||||||||
|
|
────────────┤
|
|
|
|||||||||||||||||||||||
| | ||||||||||||||||||||||||
|
|
────────────┘
|
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) { ⋮ } }