Commit 9e9478a
committed
feat: Add MethodOverriding demo with SuperMethod and SubMethod classes
WHAT the code does:
Defines SuperMethod with:
- display(): prints "Super Class Display".
Defines SubMethod extending SuperMethod with:
- Overridden display(): prints "Sub Class Display".
Defines MethodOverriding with main():
- Creates a SuperMethod object and calls display().
- Creates a SubMethod object and calls display().
- Demonstrates method overriding in action.
WHY this matters:
Shows **method overriding** in Java:
- Subclass provides its own version of a method from the superclass.
- Ensures that the most specific implementation is executed.
Highlights polymorphism at the method level.
Provides a clean example of @OverRide usage for clarity and error prevention.
HOW it works:
SuperMethod sup = new SuperMethod(); → sup.display() calls superclass method → prints "Super Class Display".
SubMethod s = new SubMethod(); → s.display() calls overridden subclass method → prints "Sub Class Display".
Tips and gotchas:
Using @OverRide annotation ensures compiler checks for correct method signature matching.
This example does not yet demonstrate polymorphic references (e.g., SuperMethod sup = new SubMethod(); sup.display();), which would show dynamic method dispatch at runtime.
Class and method names follow conventions, but MethodOverriding could be renamed MethodOverridingDemo for clarity.
Use-cases:
Educational example of method overriding in OOP.
Foundation for polymorphism and runtime method resolution.
Prepares for extending into abstract classes and interface implementations.
Short key: class-supermethod submethod method-overriding polymorphism.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 53076ec commit 9e9478a
1 file changed
+5
-9
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 | + | |
| 7 | + | |
10 | 8 | | |
11 | | - | |
12 | | - | |
| 9 | + | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
| |||
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
24 | | - | |
25 | 21 | | |
26 | 22 | | |
0 commit comments