Interfaces and Abstract Classes
- Interfaces. An interface declares capability without implementation: Payable says what, classes say how.
- implements. A class implements an interface and must supply every method: the compiler enforces the contract.
- Abstract classes. An abstract class mixes implemented behaviour with holes subclasses must fill: half library, half contract.
Dynamic Dispatch
- Polymorphism. Polymorphism lets one variable of an interface type hold any implementation: code written to the contract.
- Dynamic dispatch. A call on an interface reference resolves at runtime to the actual object’s method: dynamic dispatch.
- Program to interfaces. Declaring List instead of ArrayList keeps callers decoupled: the implementation swaps without a ripple.
Designing Flexible Object Models
- Small interfaces. Small, focused interfaces compose: a class can be Comparable and Closeable without a tangled hierarchy.
- Sealed hierarchies. A sealed class names its permitted subclasses: the compiler then knows every case that can exist.
- Design by contract. Good models document what callers may assume: preconditions and guarantees written at the interface.
Before and after this level
- Before: Level 2: Classes & Objects: Classes and Instances, Encapsulation and Constructors, Composition and Inheritance.
- Next: Level 4: Collections & Generics: Lists, Sets and Maps, Generic Types, Choosing Appropriate Collections.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Java Level 3: Interfaces & Polymorphism cover?
Interfaces and Abstract Classes, Dynamic Dispatch, Designing Flexible Object Models: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 2: Classes & Objects, which covers Classes and Instances, Encapsulation and Constructors, Composition and Inheritance.