Skip to main content

OOP Overview

The four pillars of OOP — encapsulation, inheritance, polymorphism, and abstraction — are the foundation of every Java codebase. Modern Java (16–17+) adds records and sealed classes that let you model data more concisely and safely. This page is a rapid-revision cheatsheet: all critical concepts and interview questions in under 10 minutes.

Key Concepts at a Glance

  • Class: A blueprint defining fields (state) and methods (behavior). Objects are runtime instances of a class.
  • Object: A heap-allocated instance of a class; each has its own copy of instance fields.
  • Constructor: Special method with no return type, called by new to initialize an object. Chains via this(...) or super(...).
  • this: Implicit reference to the current object instance; disambiguates fields from parameters, chains constructors.
  • Encapsulation: Hiding fields with private and exposing only a controlled public API; prevents external code from corrupting state.
  • Access modifiers: private (class only) → package-private → protected (package + subclasses) → public (everyone).
  • Immutability: State cannot change after construction; achieved with private final fields, no setters, defensive copies.
  • Inheritance: Subclass (extends) absorbs non-private members of a parent; super(...) calls the parent constructor.
  • Method overriding: Subclass redefines a parent method with the same signature; resolved at runtime (dynamic dispatch).
  • Liskov Substitution Principle (LSP): A subclass must be substitutable for its parent without breaking correctness.
  • Polymorphism: One interface, many behaviors — compile-time (overloading, resolved by parameter type) or runtime (overriding, resolved by actual object type).
  • Dynamic dispatch: JVM invokevirtual looks up the method in the actual object's vtable, not the reference type.
  • Abstraction: Exposing what an object does (contract) and hiding how (implementation); via interfaces and abstract classes.
  • Interface: Pure contract (abstract methods + optional default/static methods). A class can implement many.
  • Abstract class: Partial implementation with state; cannot be instantiated; merges interface + shared logic.
  • default method: Added in Java 8; provides a standard implementation in an interface without breaking existing implementors.
  • Record (Java 16+): Concise, implicitly-final, immutable data carrier; compiler auto-generates constructor, accessors, equals, hashCode, toString.
  • Compact constructor: Record constructor variant that omits parameters and assignments — only validation/normalization logic; compiler adds assignments automatically.
  • Sealed class/interface (Java 17+): Declares a closed set of permitted subclasses via permits; enables exhaustive switch without a default branch.
  • Pattern matching: instanceof binding (Java 16) and switch patterns (Java 21) eliminate manual casts and instanceof chains.

Quick-Reference Table

Feature / KeywordPurposeKey Notes
extendsSubclass a parent classSingle inheritance only
implementsImplement an interfaceMultiple allowed
super(...)Call parent constructorMust be first statement in subclass constructor
super.method()Call parent's overridden methodWorks from within an override
@OverrideMark an intentional overrideCompiler catches signature mismatches
abstract classPartial implementation, cannot instantiateCan have fields, constructors, concrete methods
interfacePure contract (+ default / static methods)No instance fields; multiple implementation
default (interface)Provide fallback implementationConflict = must override in implementing class
final classPrevents subclassinge.g., String, records are implicitly final
final methodPrevents overridingCan still be inherited and called
private final fieldImmutable fieldCore ingredient of immutable classes
record Point(int x, int y)Immutable data carrierAuto-generates canonical ctor, x(), y(), equals, hashCode, toString
Compact constructorValidation in recordsBody runs before auto-assignment
sealed ... permitsRestrict permitted subclassesEach permitted type must be final, sealed, or non-sealed
non-sealedRe-open a branch of a sealed hierarchyUse sparingly — reduces exhaustiveness
instanceof X xPattern matching bind (Java 16+)Combines instanceof check + cast in one step
switch (shape) { case Circle c -> ... }Pattern switch (Java 21)Exhaustive for sealed types — no default needed

Learning Path

Suggested reading order for a returning Java developer:

  1. Classes & Objects — foundation: every other OOP concept builds on how classes and objects work.
  2. Encapsulation — immediately after classes: learn to protect state and design immutable classes.
  3. Inheritance — understand extends, overriding, super, and the Liskov Substitution Principle before going further.
  4. Polymorphism — the concept interviews probe hardest; master static vs. dynamic dispatch.
  5. Abstraction — interfaces vs. abstract classes: the most practically important design decision in Java.
  6. Records (Java 16+) — modern Java; eliminates data-class boilerplate.
  7. Sealed Classes (Java 17+) — pairs with records to enable safe, compiler-verified type hierarchies.

Top 5 Interview Questions

Q1: What is the difference between method overloading and method overriding?
A: Overloading is compile-time polymorphism — same class, same method name, different parameter types; resolved by the compiler based on argument types. Overriding is runtime polymorphism — subclass redefines a method with the exact same signature; resolved at runtime by the JVM's vtable lookup on the actual object type. @Override should always mark overrides to catch mistakes at compile time.

Q2: What is the difference between an abstract class and an interface?
A: An interface is a pure contract — no instance fields, no constructors; a class can implement many. An abstract class can have instance fields, constructors, and concrete methods; a class can extend only one. Use an interface to define a capability/role across unrelated types; use an abstract class to share state and partial implementation within a closely related family. Since Java 8, default methods blur the line, but the inability to hold state remains the core distinction.

Q3: What is encapsulation and why does it matter?
A: Encapsulation bundles data and behavior into a class and restricts direct access to the data (via private fields). All mutations go through methods, which can validate, enforce invariants, and log. Without it, any code anywhere can put an object into an invalid state, making bugs nearly impossible to trace. Immutable classes (all fields private final, no setters) are the strongest form.

Q4: What is a Java record and when should you use it?
A: A record (Java 16+) is a concise, implicitly-final immutable data carrier. You declare the components in the header; the compiler auto-generates the canonical constructor, accessors (x() not getX()), equals, hashCode, and toString. Use records for DTOs, value objects, API responses, map keys — any type whose purpose is carrying data with no mutable lifecycle. Avoid records for JPA entities and classes that need to be subclassed.

Q5: What are sealed classes and what problem do they solve?
A: Sealed classes (Java 17+) explicitly list the only permitted subclasses via permits. This creates a closed type hierarchy where the compiler knows every possible subtype. The key benefit: switch expressions on sealed types can be exhaustive — no default branch required, and adding a new permitted subtype causes a compile error in any switch that doesn't handle it. This replaces fragile instanceof chains with compiler-verified type dispatch. The idiomatic pattern pairs a sealed interface with record implementations for each variant.


All Notes in This Domain

NoteDescription
Classes & ObjectsBlueprints vs. instances — fields, methods, constructors, this, static vs. instance members.
EncapsulationAccess modifiers, getters/setters, immutable classes, defensive copying.
Inheritanceextends, overriding, super, LSP, fragile base class, composition vs. inheritance.
PolymorphismCompile-time (overloading) vs. runtime (overriding), dynamic dispatch, programming to interfaces.
AbstractionAbstract classes vs. interfaces, default methods, diamond problem, interface segregation.
Records (Java 16+)Immutable data carriers, compact constructors, canonical constructors, record patterns.
Sealed Classes (Java 17+)Closed hierarchies, permits, final/sealed/non-sealed modifiers, exhaustive pattern matching.