B3.2.4 · OOP for multiple classes
Abstract classes & interfaces
By the end you can
Design systems with abstract classes and architectural interface contracts to enforce polymorphic behavior.
In large-scale system design, architects frequently define common behaviors that multiple different classes must share, without committing to how those behaviors are carried out. Two complementary mechanisms achieve this in object-oriented design: and .
1. Abstract classes#
An abstract class sits at the top of an inheritance hierarchy to provide a shared template:
- Cannot be instantiated: You can never write
new Shape(). Doing so generates a compilation error. - Can contain concrete code: An abstract class can contain regular instance variables, constructors, and fully implemented methods that subclasses inherit.
- Abstract methods: Methods declared with the
abstractkeyword have a signature but no body (e.g.public abstract double area();). Every non-abstract subclass is forced by the compiler to override and implement them.
2. Interfaces: Pure contracts#
An interface is a pure contract specifying what an object can do, with zero concern for how it does it:
// Interface declaration:
public interface Printable {
void print(); // Implicitly public and abstract
}
// Class implementation:
public class Invoice implements Printable {
public void print() {
System.out.println("Printing customer invoice...");
}
}A single class can implement multiple interfaces (e.g. class Player implements Controllable, Serializable, Renderable). This provides the architectural benefits of multiple inheritance while avoiding the diamond problem of conflicting field implementations.
Check yourself
Abstract classes and interfaces
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 8 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Compare an abstract class and an interface in Java in terms of instantiation, method implementations, inheritance limits, and attributes.
[4]Compare: Similarities and differences, about both things, point by point.
2.Explain what is meant by saying that an interface acts as a 'contract' in software architecture.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.Describe the purpose of declaring an abstract method without a body in a superclass.
[2]Describe: A detailed account: say what happens, step by step if it is a process.
In a nutshell
- An abstract class is an incomplete class that cannot be directly instantiated and defines abstract methods for subclasses to implement.
- An interface is a pure architectural contract declaring method signatures without instance variables.
- Java classes can extend only one superclass (abstract or concrete), but can implement multiple interfaces simultaneously.
- Interfaces enable loose coupling and dependency injection across diverse software components.
