Skip to content

Commit b127c8f

Browse files
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

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
class Supermethod
2-
{
3-
public void display()
4-
{
1+
class Supermethod {
2+
public void display() {
53
System.out.println("Super Class Display");
64
}
75
}
86

9-
class SubMethodOverride extends SuperMethod
10-
{
11-
public void display()
12-
{
7+
class SubMethodOverride extends SuperMethod {
8+
public void display() {
139
System.out.println("Sub class Display");
1410
}
1511
}
1612

17-
public class MethodOverriding1
18-
{
19-
public static void main(String[] args)
20-
{
13+
public class MethodOverriding1 {
14+
public static void main(String[] args) {
2115
SuperMethod sup = new SubMethod();
2216
sup.display(); //It is called Dynamic Method Dispatch.
2317

@@ -27,4 +21,4 @@ public static void main(String[] args)
2721
SubMethodOverride s= new SubMethodOverride();
2822
s.display();
2923
}
30-
}
24+
}

0 commit comments

Comments
 (0)