01
Classes and Interfaces
Object lifecycle and constructors, encapsulation through visibility levels, where overriding and hiding come apart, the choice between an abstract class and an interface, class-level state and initialization order, the extension restriction, nested class forms, and data-carrier types.
- 01 Class, Object, and Constructor The constructor chain runs top to bottom, and an overridden method called from the chain's upper link comes from the runtime type: since the subclass's fields are not yet assigned, the result comes out `sub: null x0`; in a fragile version the object is never delivered — the trap does not arise in a private or final method.
- 02 Access Modifiers Crossing four visibility levels with four call positions, ten of sixteen pairs compile; the whole decision is made at compile time and the member stays right where it is at runtime — all four fields sit in the class file, and only a flag carries the distinction. Protected level also opens up the package.
- 03 Inheritance and Overriding Eight decisions are read on a single reference and the answer splits right down the middle: four come from the runtime type, four from the declared type. Redeclaring fields under the same name is not overriding, it is hiding; once the signature changes, overriding never arises at all and the decision switches sides.
- 04 Abstract Classes and Interfaces The same capability is set up eight separate ways, and compilation stops on three of them; on the remaining five the compiler decides on its own and never asks which side — a class body beats an interface default, a subinterface beats the one above it, and only two independent interfaces giving the same default get rejected. A state-carrying capability cannot be set up with an interface.
- 05 Static Members and Initialization Blocks A static method is not overridden, it is hidden: the same call spelling is picked from the declared type, and a static call made through a null reference even works. The class-level state's single copy, which access triggers class initialization, and the case where the same line goes to two separate sides are measured.
- 06 The `final` Keyword A single keyword places three separate restrictions and enforces all three at compile time: reassignment, overriding, and extension. A constant final field's value is written into the reading class, and the old value stays in place when the source changes; a final reference, in turn, does not freeze the object.
- 07 Inner and Local Classes A single source file produces six class files, and four of those carry hidden fields never written in the source. An inner class binds to the enclosing instance through a field and reads that field live; a local class copies the variable it captures through a constructor parameter, which is why that variable can never be reassigned.
- 08 Records and Enumerations A one-line record declaration produces ten members in the class file, and the produced class cannot be extended, meaning its declared type and runtime type can never come apart. In an enum, a constant with its own body written takes its own class; a record's produced equality, in turn, protects only shallowly.