library
beginnerPolymorphism: Compile-Time & Runtime
Distinguish between static (overloading) and dynamic (overriding) polymorphism and know when each applies.
Polymorphism means one name, several behaviours. Java has two kinds, and they are resolved at different times.
Overloading = same dish in different sizes (you pick at order time). Overriding = franchise locations cooking the same dish differently (depends on which location you visit).
Key Concepts
1
Compile-time (static) overloading same name, different parameters
Runtime (dynamic) overriding subclass replaces the implementation2
Overloading: the compiler picks.
void print(int x) { ... }
void print(String s) { ... }3
print(5); // print(int), chosen while compiling4
Overriding: the object picks.
class Shape { double area() { return 0; } }
class Circle extends Shape {
double area() { return Math.PI * r * r; }
}5
Shape s = new Circle(2);
s.area(); // Circle's version, chosen while running6
That difference is the whole question. Overloading looks at the reference and the argument types.
Overriding looks at the actual object.
7
When both appear together, they happen in that order. The compiler fixes the signature, then the JVM
dispatches to the right implementation of it:
8
void draw(Shape s) { ... }9
Shape s = new Circle(2);
draw(s); // compiler: draw(Shape). then s.area() -> Circle's10
Static methods are not polymorphic. A subclass declaring the same static signature hides it rather
than overriding it:
11
class A { static String who() { return "A"; } }
class B extends A { static String who() { return "B"; } }12
A x = new B();
x.who(); // "A" -- the reference, not the object13
Which is why @Override on a static method will not compile. The annotation is a useful check here: it
fails whenever you only thought you were overriding.
@Override
14
What the interviewer is probing.1. "Is method overloading polymorphism?" Probing: whether you know the two kinds and the
terminology argument. Stalls: "No, only overriding counts." Moves up: overloading is
compile-time polymorphism, resolved by the compiler; overriding is runtime, resolved by the object.
Both are polymorphism, at different times.
15
2. "A static method is redeclared in a subclass. What happens when you call it through a parent
reference?" Probing: method hiding. Stalls: "The subclass version runs." Moves up: static
methods are hidden, not overridden, so dispatch follows the reference type and the parent version
runs. @Override will not compile on it.
16
3. "How does the JVM actually dispatch an overridden call?" Probing: whether you can go one
level below the syntax. Stalls: "It just knows the type." Moves up: each class has a table of
method pointers, and invokevirtual looks the method up in the actual object's table at runtime.
17
4. "Where does overloading get resolved when both overloading and overriding are in play?"
Probing: the order of the two mechanisms. Stalls: "Whichever is more specific." Moves up: the
compiler picks the signature first, then the JVM dispatches to the right implementation of that
signature — compile time, then runtime.