LOW LEVEL DESIGN · STEP 1 OF 9
Object-oriented programming
Classes, objects and the four pillars, written the way interviewers expect. Each lesson is an animated walkthrough with a small Java example you can run, notes and the interview questions with answers.
- 1 Classes and objectsWhat a class and an object really are, shown with a small ride app: state and behaviour kept together, constructors that make every object valid from birth, many instances from one blueprint, references, static versus instance members, and the basics of equals and hashCode, with a runnable Java 17 example.
- 2 EncapsulationHow encapsulation keeps an object correct no matter who calls it, shown with a digital wallet: private fields, invariants the object promises to keep, behaviour methods instead of setters, the hidden leak of returning a mutable list, defensive copies in and out, immutable values, and access modifiers, with a runnable Java 17 example.
- 3 AbstractionWhat abstraction means in object-oriented design, shown with parcel couriers: a checkout that knows only a Courier contract, interfaces and abstract types as promises, swapping and faking implementations, keeping each method at one level of abstraction, leaky abstractions, how abstraction differs from encapsulation, and when not to add one, with Java 17 code.
- 4 InheritanceHow inheritance works in Java and where it goes wrong, shown with enemies in a small game: the is-a test, extends and super, constructor chaining, overriding rules, protected members, final, single inheritance, the fragile base class problem where a harmless change in a parent breaks a child, and misuse inside the JDK itself, with Java 17 code.
- 5 PolymorphismPolymorphism explained with ride fares: one call that behaves differently for each ride type, runtime polymorphism through overriding and dynamic dispatch, compile-time overloading and its trap, why fields and static methods are not polymorphic, and how to replace a growing if-else chain on a type code with classes, with a runnable Java 17 example.
- 6 Interfaces vs abstract classesWhen to use an interface and when to use an abstract class, shown with a library system: what each one can hold, default, static and private interface methods, state and constructors in abstract classes, implementing many interfaces, the default method clash and how to resolve it, the interface plus skeleton class combo, and a simple rule for choosing, with Java 17 code.
- 7 Composition over inheritanceWhy designers prefer composition to inheritance, shown with delivery riders: the subclass explosion when two kinds of variation multiply, has-a relationships and delegation, swapping behaviour at runtime, easier testing with fakes, escaping the fragile base class, the patterns built on it, and when inheritance is still the right call, with Java 17 code.