Commit 75a46a6
committed
feat: Add OverridingRules demo with SuperOverride and SubOverride classes
WHAT the code does:
Defines SuperOverride with:
- DisplayOverride(): prints "Super Method Display."
Defines SubOverride extending SuperOverride with:
- Overridden DisplayOverride(): prints "Sub Display."
Defines OverridingRules with main():
- Creates a SuperOverride reference to a SubOverride object → calls overridden method.
- Creates standalone SuperOverride and SubOverride objects and calls methods.
- Demonstrates multiple reference/object combinations.
WHY this matters:
Illustrates **method overriding** and **dynamic method dispatch** in Java:
- When a superclass reference points to a subclass object, the subclass method executes.
Shows how method resolution depends on the actual object type, not just the reference type.
Reinforces that overriding allows specialization of behavior in subclasses.
HOW it works:
SuperOverride s = new SubOverride(); → prints "Sub Display."
SuperOverride s1 = new SuperOverride(); → prints "Super Method Display."
SubOverride s2 = new SubOverride(); → prints "Sub Display."
SuperOverride s3 = new SuperOverride(); → prints "Super Method Display."
Tips and gotchas:
@ Override annotation should be added in SubOverride to ensure proper overriding.
Method naming conventions: use lowerCamelCase (displayOverride) for clarity.
This example uses identical method signatures—overriding requires same name, parameters, and return type.
Overloading (different parameters) is different from overriding (same signature, different behavior).
Use-cases:
Educational example of overriding rules in OOP.
Foundation for learning polymorphism and runtime behavior resolution.
Useful stepping stone for exploring abstraction and interface-based polymorphism.
Short key: class-superoverride suboverride method-overriding dynamic-dispatch.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5e462ef commit 75a46a6
1 file changed
+6
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
8 | 6 | | |
9 | | - | |
10 | | - | |
| 7 | + | |
| 8 | + | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
| |||
25 | 23 | | |
26 | 24 | | |
27 | 25 | | |
28 | | - | |
29 | 26 | | |
30 | 27 | | |
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
34 | 31 | | |
35 | 32 | | |
36 | | - | |
| 33 | + | |
37 | 34 | | |
38 | 35 | | |
39 | 36 | | |
40 | 37 | | |
41 | 38 | | |
42 | | - | |
| 39 | + | |
43 | 40 | | |
44 | 41 | | |
45 | 42 | | |
46 | | - | |
| 43 | + | |
0 commit comments