Inheritance, interfaces, records and sealed types
The part that changed most in recent Java : extends
and super , overriding and the dynamic dispatch
of a supertype reference, abstract classes and the template method,
interfaces with default and static methods, and interfaces that extend
interfaces. Then Object and its methods,
final on a class, a method and a field, and
records: the canonical constructor, accessors, equals
for free and a compact constructor. Records meet patterns in
instanceof and in a switch ,
and enums gain a field and a method per constant. Sealed types close a
hierarchy, and the Capstone builds a shape hierarchy over it, with
composition, interfaces and the old Java of
Vector read in the last lesson.
5.1
Inheritance
extends, super, overriding, the constructor chain, @Override
Open →
5.2
Polymorphism
a supertype reference, dynamic dispatch, an array of the supertype, instanceof
Open →
5.3
Abstract classes
an abstract method, a concrete method, the template method, the class that cannot be instantiated
Open →
5.4
Interfaces
implements, a method, a default method, a static method, two interfaces at once
Open →
5.5
Interfaces that extend interfaces
extends between interfaces, a diamond of defaults and the class that must override
Open →
5.6
Object and its methods
the methods every class inherits, toString, equals, hashCode, getClass, the ones to override
Open →
5.7
final classes and methods
final on a class, on a method, on a field, and the design reason
Open →
5.8
Records
record, the canonical constructor, accessors, equals and hashCode for free, a compact constructor
Open →
5.9
Records and pattern matching
a record in instanceof, deconstruction in a switch
Open →
5.10
Enums with behaviour
an abstract method per constant, a field per constant, EnumSet, switch over an enum
Open →
5.11
Sealed types
sealed, permits, non-sealed, a closed hierarchy of records
Open →
5.12
instanceof patterns
the pattern variable, its scope, a guard, the old cast rewritten
Open →
5.13
Patterns in switch
type patterns, null as a label, exhaustiveness, the selector that must be sealed
Open →
5.14
A closed record hierarchy
a sealed interface of records, an exhaustive switch, a total function over it
Open →
5.15
Composition against inheritance
the same feature both ways, the fragile base class, the refactor
Open →
5.16
Interfaces against abstract classes
state, constructors, multiple inheritance of type, a table with a program each
Open →
5.17
Capstone: a shape hierarchy
an interface, three records, area, a sorted report, instanceof and a switch
Open →
5.18
Reading old Java
Vector, Hashtable, Enumeration, raw types, the warnings -Xlint:all prints, a modernised class
Open →
Lessons
course/
Inheritance, interfaces, records and sealed types (18)
Capstone: a bank account
Inheritance