6.1 extends — IS-A Relationships
Inheritance lets a subclass acquire the fields and methods of a superclass, modelling an IS-A relationship (Dog extends Animal). Java supports single class inheritance only (one direct superclass) — the C++ diamond problem for state is designed out; multiple inheritance of type comes via interfaces (Lesson 8).
Types examiners list: single, multilevel (A→B→C), hierarchical (one parent, many children). Multiple and hybrid are supported only through interfaces. Private members are inherited in memory but not accessible; constructors are never inherited.
6.2 super — Three Uses
super.field— access a shadowed superclass field.super.method()— call the overridden superclass version.super(args)— invoke a superclass constructor; must be the first statement.
Constructor chaining: every constructor implicitly begins with super() unless you write super(...)/this(...) yourself. If the parent has no no-arg constructor, the child must call super(args) explicitly or compilation fails — a classic trick question.
class A { A() { System.out.println("A ctor"); } }
class B extends A { B() { System.out.println("B ctor"); } }
new B();
// Output: A ctor then B ctor (parent constructs first, always)
6.3 Overriding vs Overloading — The Definitive Table
| Aspect | Overloading | Overriding |
|---|---|---|
| Where | Same class (or inherited set) | Subclass redefines parent method |
| Signature | Same name, different parameters | Identical name + parameters |
| Return type | Can differ freely | Same or covariant (subtype) |
| Binding | Compile time (static) | Runtime (dynamic dispatch) |
| Polymorphism kind | Compile-time | Runtime |
static methods | Can be overloaded | Hidden, not overridden |
private/final | Can be overloaded | Cannot be overridden |
Overriding rules: access modifier may not be more restrictive (protected → public is fine; public → protected is an error); cannot throw broader checked exceptions; use @Override so the compiler catches typos (e.g., equals(Object) vs a mistaken equals(MyClass) overload).
Fields are never overridden — field access is resolved by the reference type at compile time (field hiding), while instance-method calls are resolved by the object type at runtime. Predict-the-output papers exploit this relentlessly.
6.4 The Object Class — Root of Everything
Every class implicitly extends java.lang.Object. Methods you must know:
| Method | Purpose | Typical action |
|---|---|---|
toString() | String form; default is ClassName@hexHash | Override for readable printing |
equals(Object) | Logical equality; default is == | Override for value comparison |
hashCode() | Bucket index for hash collections | Must override with equals |
getClass() | Runtime class object | Reflection |
clone() | Field-by-field copy (needs Cloneable) | Shallow by default |
wait()/notify()/notifyAll() | Thread coordination (Lesson 11) | Call only inside synchronized |
finalize() | Pre-GC hook | Deprecated |
The equals/hashCode contract: if a.equals(b) then a.hashCode() == b.hashCode() must hold. Violate it and your objects vanish inside HashMap/HashSet — an advanced-paper favourite.
6.5 final — Sealing Things
- final variable: constant after assignment (blank finals may be set once in the constructor). For references, the reference is fixed — the object's contents can still mutate.
- final method: cannot be overridden (also enables inlining).
- final class: cannot be extended — e.g.,
String,Integer, all wrapper classes.
Why is String final? To protect the string pool, its cached hash code, and security-sensitive uses (class loading, file paths) from malicious subclassing.
🎯 Exam Focus
- Explain types of inheritance in Java. Why does Java not support multiple inheritance through classes? How do interfaces solve it?
- List the three uses of
superwith examples. Trace constructor-chaining output for a three-level hierarchy. - Differentiate method overloading and method overriding (any six points) with programs.
- State the rules of overriding regarding access modifiers, covariant return types and checked exceptions.
- Explain the equals()/hashCode() contract. What goes wrong in a HashSet if only equals() is overridden?
- Write a program with class Employee (name, salary) and subclass Manager (bonus) that overrides
toString()andequals()correctly.