Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| encapsulation | hide data; expose rule-enforcing methods | 1 |
| invariant | a rule that must always hold for an object | 1 |
| private / package-private / protected / public | class / package / package + subclasses / everyone | 1 |
| getter / setter | method that reads / writes a field | 1 |
inheritance / extends | reuse and extend a class ("is-a") | 2 |
| superclass / subclass | parent / child | 2 |
super(...) / super.m() | call parent constructor / parent's method | 2, 3 |
Object | the root class of all Java classes | 2 |
| single inheritance | a class extends only one class | 2 |
overriding / @Override | subclass replaces an inherited method / compiler check | 3 |
| covariant return | overriding method may return a subtype | 3 |
| overloading vs overriding | different params, compile time / same params, run time | 3 |
| polymorphism | one call, behaviour depends on the actual object | 4 |
| declared type / actual type | what the compiler knows / what the object really is | 4 |
| dynamic dispatch / vtable | choosing the method at run time / per-class method table | 4 |
| upcast / downcast / ClassCastException | to parent (safe) / to child (checked) / invalid downcast | 4 |
instanceof pattern | if (x instanceof T t) test-and-bind | 4 |
| stack frame / heap | a method's private notepad for locals / shared area where all objects live | 4 |
| object header | hidden start of every object; points to its class | 4 |
invokevirtual / invokeinterface | bytecode calls whose version is chosen at run time | 4 |
| static binding / dynamic binding | method fixed at compile time / chosen at run time | 4 |
| monomorphic / megamorphic call site | the JIT saw one type there / many types | 4 |
ArrayStoreException | storing the wrong type into a covariant array | 4 |
| tell, don't ask | call an overridden method instead of checking types | 4 |
| abstraction | expose what, hide how | 5, 6 |
| abstract class / abstract method | can't instantiate / no body, must be implemented | 5 |
| template method | parent defines steps; subclasses fill in some | 5 |
interface / implements | contract ("can-do") / signing it | 6 |
| default / static interface method | inherited body / utility on the interface | 6 |
| diamond problem | ambiguity from two parents with the same member | 6 |
| programming to an interface | depend on the interface type, not the class | 6 |
| composition / delegation | "has-a" fields / forwarding work to them | 7 |
| fragile base class | parent changes breaking subclasses | 7 |
| equals/hashCode contract | equal objects ⇒ equal hash codes | 8 |
| hash collision | unequal objects, same hash code | 8 |
| static / instance initializer | runs once per class / per object | 9 |
| final class / final method | can't be extended / overridden | 9 |
| static nested / inner / anonymous class | independent helper / tied to outer object / unnamed one-off | 10 |
enum / values() / ordinal() | fixed set of instances / all of them / position | 11 |
| record / compact constructor | one-line immutable data class / its validation block | 12 |
Cheat Sheet#
Templates
abstract class Base { interface Capability {
protected final int x; int must();
Base(int x) { this.x = x; } default String nice() { return "v" + must(); }
abstract int step(); }
int run() { return step() * 2; }
}
class Impl extends Base implements Capability {
Impl() { super(1); }
@Override int step() { return x + 1; }
@Override public int must() { return 42; }
}
record Cell(int r, int c) {} enum Level { LOW, MID, HIGH }equals / hashCode
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point p)) return false;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
} // same fields as equals
// or just: record Point(int x, int y) {}Decision rules
data private, behaviour public · meaningful methods > raw setters
interface for the type others depend on · abstract class only for shared state/code
composition by default · inheritance only for stable, substitutable is-a
override equals ⇒ override hashCode · immutable keys · records for data
many instanceof checks ⇒ move behaviour into overridden methods
nested classes static unless they need the outer objectTry This Yourself#
Add a question type. Add
FillInTheBlank(correct if the answer matches any of several accepted words) to the quiz example. Confirm the grading loop needed zero changes.Break the contract on purpose. Write
record Cell(int r, int c) {}and a normal classCellClasswith onlyequalsoverridden. Put(2, 3)into aHashSetof each and checkcontains(new ...(2, 3)). Then addhashCodeto the class and try again.Composition drill. Implement
TimerasNoLimitTimerandCountdownTimer(int seconds), andProctorasNoProctorandTabSwitchProctor. Build four different quizzes from these parts without creating any newQuizsubclasses.Explain out loud (2 minutes each, recorded): the four pillars; abstract class vs interface; why
@Overridematters.