library
beginnerInheritance & Method Resolution
Understand how Java resolves which method to call when classes form an inheritance hierarchy.
A subclass gets the superclass's fields and methods. Java allows one superclass and any number of interfaces.
A family recipe: the child inherits grandma's recipe but can modify it (override). When you ask the child to cook, they use their version.
Key Concepts
1
The rule people get wrong: overriding is decided at runtime, overloading at compile time.
2
class Animal { void speak() { print("..."); } }
class Dog extends Animal { void speak() { print("Woof"); } }3
Animal a = new Dog();
a.speak(); // "Woof" -- the OBJECT decides4
The reference says Animal, but the object is a Dog, and overriding follows the object. This is dynamic
dispatch, and the JVM does it through a table of method pointers per class.
AnimalDog
5
Overloading is the opposite. It picks a signature from the reference type:
6
void feed(Animal x) { print("animal"); }
void feed(Dog x) { print("dog"); }7
Animal a = new Dog();
feed(a); // "animal" -- the REFERENCE decides8
Nothing is looked up at runtime here. The compiler chose feed(Animal) and that is what runs.
feed(Animal)
9
Constructors chain upward. Every constructor calls a superclass constructor first:
10
class Dog extends Animal {
Dog(String name) {
super(name); // implicit super() if you omit it
}
}11
If the superclass has no no-argument constructor, you must call super(...) yourself, or it will not
compile.
super(...)
12
super reaches the parent's version of a method you have overridden:
super
13
void speak() { super.speak(); print("Woof"); }14
final blocks it. On a method, no subclass can override. On a class, nothing can extend it, which is
why String is final.
finalString
15
Deep hierarchies hurt. Past about three levels, a change in the base class breaks children in ways
nobody predicted. Prefer composition unless the relationship really is "is-a".
16
What the interviewer is probing.1. "A variable is declared Animal and holds a Dog. Which speak() runs, and which feed()
overload?" Probing: overriding versus overloading, the perennial question. Stalls: "Both use
the Dog." Moves up: overriding follows the object so Dog.speak() runs; overloading is chosen by
the compiler from the reference type, so feed(Animal) is selected.
17
2. "What happens if the superclass has no no-argument constructor?" Probing: constructor
chaining. Stalls: "It still works." Moves up: the implicit super() call fails to compile, so the
subclass must call super(...) explicitly as its first statement.
18
3. "Why is a deep inheritance hierarchy a problem?" Probing: whether you have felt the fragile
base class issue. Stalls: "It gets confusing." Moves up: subclasses depend on how the parent is
implemented, not just what it promises, so a parent change breaks children in ways nobody predicted.
19
4. "Can a constructor be overridden?" Probing: whether you know constructors are not
inherited. Stalls: "Yes, by redefining it." Moves up: no — constructors are not inherited, so a
same-named constructor in a subclass is simply that subclass's own.