Commit ce9533f
committed
feat: Add DynamicMethodDispatch example with method overriding and polymorphism
WHAT the code does:
Defines Super with methods:
- meth1(): prints "Super Meth1".
- meth2(): prints "Super Meth2".
Defines Sub extending Super:
- Overrides meth2() to print "Sub Meth2".
- Adds meth3() as a new method.
Defines DynamicMethodDispatch with main():
- Creates a Super reference pointing to a Sub object.
- Calls meth1() and meth2() on the reference, demonstrating polymorphism.
WHY this matters:
Demonstrates dynamic method dispatch (runtime polymorphism) in Java:
- Method calls are resolved at runtime based on the actual object type, not the reference type.
Illustrates method overriding with @OverRide for correctness and clarity.
Shows how subclass-specific methods (like meth3()) cannot be called on a superclass reference.
HOW it works:
Super sup = new Sub();
sup.meth1() → resolved to Super.meth1 → prints "Super Meth1".
sup.meth2() → resolved to Sub.meth2 → prints "Sub Meth2".
sup.meth3() → not allowed, since reference type Super doesn’t define meth3().
If a Sub reference were used directly, meth3() would also be accessible.
Tips and gotchas:
Use @OverRide to catch accidental mistakes in overriding method signatures.
Dynamic method dispatch applies only to overridden methods, not to fields (field access depends on reference type).
System.err is used in Sub.meth2(); this prints to standard error, which may look different from System.out in some consoles.
Polymorphism is powerful for designing flexible systems but can be misused if hierarchies grow unnecessarily deep.
Use-cases:
Educational demonstration of runtime polymorphism in Java.
Foundation for learning abstraction and interface-based designs.
Practical basis for frameworks where behavior is decided at runtime via object type.
Short key: class-super-sub dynamic-method-dispatch polymorphism.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9f45134 commit ce9533f
1 file changed
+45
-38
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 | | - | |
9 | | - | |
| 6 | + | |
10 | 7 | | |
11 | 8 | | |
12 | 9 | | |
13 | 10 | | |
14 | | - | |
15 | | - | |
| 11 | + | |
16 | 12 | | |
17 | | - | |
18 | | - | |
| 13 | + | |
19 | 14 | | |
20 | 15 | | |
21 | 16 | | |
22 | | - | |
23 | | - | |
| 17 | + | |
24 | 18 | | |
25 | 19 | | |
26 | 20 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
31 | 24 | | |
32 | 25 | | |
33 | 26 | | |
34 | 27 | | |
35 | 28 | | |
36 | 29 | | |
37 | | - | |
| 30 | + | |
38 | 31 | | |
39 | 32 | | |
40 | 33 | | |
41 | 34 | | |
42 | 35 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | 36 | | |
54 | 37 | | |
55 | 38 | | |
56 | 39 | | |
57 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
61 | 60 | | |
62 | 61 | | |
63 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
64 | 65 | | |
65 | | - | |
66 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
67 | 72 | | |
68 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
69 | 76 | | |
70 | 77 | | |
71 | 78 | | |
72 | | - | |
73 | | - | |
| 79 | + | |
74 | 80 | | |
| 81 | + | |
| 82 | + | |
75 | 83 | | |
76 | | - | |
77 | | - | |
| 84 | + | |
| 85 | + | |
78 | 86 | | |
79 | | - | |
80 | | - | |
| 87 | + | |
0 commit comments