Commit b127c8f
committed
feat: Add MethodOverriding1 demo with dynamic method dispatch
WHAT the code does:
Defines Supermethod with:
- display(): prints "Super Class Display".
Defines SubMethodOverride extending Supermethod with:
- Overridden display(): prints "Sub class Display".
Defines MethodOverriding1 with main():
- Creates a Supermethod reference pointing to a SubMethodOverride object and calls display().
- Creates a standalone Supermethod object and calls display().
- Creates a standalone SubMethodOverride object and calls display().
WHY this matters:
Demonstrates **method overriding** and **dynamic method dispatch**:
- When a superclass reference holds a subclass object, the overridden method in the subclass is executed at runtime.
Reinforces polymorphism in Java, showing how behavior depends on the actual object type, not the reference type.
HOW it works:
Supermethod sup = new SubMethodOverride();
- sup.display() resolves to SubMethodOverride.display() → prints "Sub class Display".
Supermethod sup1 = new Supermethod();
- sup1.display() → prints "Super Class Display".
SubMethodOverride s = new SubMethodOverride();
- s.display() → prints "Sub class Display".
Tips and gotchas:
There’s a naming mismatch: class is declared as Supermethod but referenced as SuperMethod in SubMethodOverride → this won’t compile without correction.
@OverRide annotation should be used in SubMethodOverride to enforce correct overriding.
Dynamic method dispatch applies only to overridden methods; fields are resolved by reference type, not object type.
Naming conventions: stick to consistent capitalization (Supermethod vs SuperMethod).
Use-cases:
Educational demonstration of runtime polymorphism in Java.
Shows method overriding across different reference and object combinations.
Foundation for understanding frameworks where runtime object types drive behavior.
Short key: class-supermethod submethodoverride method-overriding dynamic-dispatch.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9e9478a commit b127c8f
1 file changed
+7
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 7 | + | |
| 8 | + | |
13 | 9 | | |
14 | 10 | | |
15 | 11 | | |
16 | 12 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 13 | + | |
| 14 | + | |
21 | 15 | | |
22 | 16 | | |
23 | 17 | | |
| |||
27 | 21 | | |
28 | 22 | | |
29 | 23 | | |
30 | | - | |
| 24 | + | |
0 commit comments