Skip to content

Commit 9e9478a

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

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
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
}
8-
class SubMethod extends SuperMethod
9-
{
6+
7+
class SubMethod extends SuperMethod {
108
@Override
11-
public void display()
12-
{
9+
public void display() {
1310
System.out.println("Sub Class Display");
1411
}
1512
}
@@ -21,6 +18,5 @@ public static void main(String[] args) {
2118

2219
SubMethod s= new SubMethod();
2320
s.display();
24-
2521
}
2622
}

0 commit comments

Comments
 (0)