Skip to content
academia.sh

Course Intermediate

Object-Oriented Java

By the end of this course

Start course

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.

02

Generics and Exceptions

What type parameters do at compile time and the gap type erasure leaves at runtime, separating read and write direction with bounded wildcards, the checked/unchecked exception distinction, automatic resource closing, and the decision of whether an error gets reported as an exception or as a return value.

  1. 01 Generic Types The type parameter gets written to the class file in two separate places: erased in the descriptor the virtual machine reads, present in the signature attribute. The measurement shows the consequence — the check exists only at compile time, two generic types are the same class at runtime, and a write made through the raw type falls at the read site.
  2. 02 Bounded Wildcards Four container forms and two operations give eight pairs; only four of them compile. Covariance opens reading, contravariance opens writing, and the two never open together; an unbounded wildcard, in turn, allows only writing a null value. The entire direction bound lives in the signature attribute, and leaves no trace at runtime.
  3. 03 Exception Hierarchy What the checked/unchecked exception distinction forces at compile time gets measured: only one of nine writings gets rejected, the declaration sits in the class file as an attribute, not an instruction, and the virtual machine never checks it. Which catch branch runs, though, is decided by the runtime type.
  4. 04 Try-with-Resources When the body and the close fail at the same time, the hand-written form fails to reach one of the two events, while try-with-resources reaches both. The measurement shows, together, the count of lost and kept exceptions, closing's reverse order, and the extra five instructions the compiler generates.
  5. 05 Exception Design The same work gets built with three contracts: a checked exception, an unchecked exception, and a result type. The measurement shows the count of decision points the compiler forces on the calling side, and what a careless caller gets from an empty store; none of the three contracts eliminates the defect, they only move where it shows up.

Start typing to search.

↑↓ Esc navigate · open · close