1. Class Fundamentals
- Class: blueprint — fields, methods, constructors, nested types.
- Constructor: has no return type, name matches class; default constructor is provided only if none is declared.
staticmembers belong to the class, shared across all instances, loadable without an instance.finalfield: must be assigned exactly once (via initializer or constructor).- Method overloading: same name, different parameter list — resolved at compile time.
- Method overriding: same signature in subclass → resolved at runtime (dynamic dispatch).
Gotcha: overload resolution picks the most specific applicable type at compile time. null is ambiguous between two ref types if both are applicable — compiler error unless one is more specific.
2. Inheritance & Polymorphism
extendsfor a single superclass. Java is single inheritance for classes.thisvssuper:this(...)chains constructors;super(...)calls a parent constructor — must be the first statement.- Method dispatch: overridden method chosen by the runtime type of the object, not the declared type.
- Private methods are not overridden — they’re bound statically; overriding a private method in a subclass is just a new method.
- Static methods are hidden, not overridden — call resolution depends on the compile-time type.
finalmethod/class: cannot be overridden/extended.
class Animal {
void sound() { System.out.println("generic"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("bark"); }
}
Animal a = new Dog();
a.sound(); // "bark" — dynamic dispatch
3. Abstract Classes vs Interfaces
| Abstract class | Interface | |
|---|---|---|
extends | single | implements multiple |
| State | can hold instance fields | fields are public static final (constants) |
| Constructors | yes | no |
| Methods | can be concrete or abstract | default/static/private methods possible, others abstract |
private helper | yes | yes (Java 9+) |
| When to use | shared state/behaviour among is-a subclasses | capabilities/contract across unrelated classes |
- Before Java 8 interfaces were pure abstracts — legacy interview point.
- Default methods let you add behaviour without breaking implementers; diamond problem from two defaults is resolved by overriding.
3. equals() & hashCode() Contract
- Reflexive:
a.equals(a)→ true. - Symmetric:
a.equals(b)⇔b.equals(a). - Transitive, consistent, not-null safe (
x.equals(null)→ false). - The critical rule: equal objects must have equal hashCodes. Unequal hash ≠ wrong, equal hash ≠ equal (collisions are allowed).
- Why it matters:
HashMap,HashSet,Hashtableuse hash → bucket; a bad hashCode breaks lookups/duplicate detection.
public class Person {
private final String name;
private final int age;
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person p)) return false;
return age == p.age && name.equals(p.name);
}
public int hashCode() { return 31 * name.hashCode() + age; }
}
Interview tip: always override both together — after overriding equals you must override hashCode or collections break. This pairing is the single most-tested OOP detail.
4. The String Family
| String | StringBuilder | StringBuffer | |
|---|---|---|---|
| Mutable | no | yes | yes |
| Thread-safe | yes | no | yes |
| Use for | literals, immutability | single-threaded concat | multi-threaded concat |
| Speed | slowest in loops | fastest | slower (sync) |
StringBuilderandStringBufferhave identical API — only thread-safety differs.+concatenation compiles intoStringBuildercalls — fine for a few, wasteful in loops.- String pool: literals are interned.
new String(...)skips the pool → poll activity. Use.intern()consciously (rarely needed; memory cost).
Memory gotcha: str.substring() before Java 7 kept the parent char[] — modern Java copies, so no memory leak.
5. Nested Classes: static vs inner
- Static nested —
static class Nested: living inside outer, no reference to the outer instance. - Inner (non-static) — has an implicit outer reference; cannot be created without an outer instance; can access outer private fields.
- Local class — inside a method.
- Anonymous class — one-shot impl without a name.
- Lambdas replaced most anonymous
Runnable/Comparatorimplementations.
Gotcha: inner class instance retains the outer instance → memory leak risk when the inner outlives the outer (e.g., kept in a cache).
Premium Content
Unlock Part 2: OOP & Core Classes and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans