Skip to content

Commit 75a46a6

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

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

Section12Inheritance/src/OverridingRules.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
class SuperOverride{
2-
public void DisplayOverride()
3-
{
2+
public void DisplayOverride() {
43
System.out.println("Super Method Display.");
54
}
65
}
7-
class SubOverride extends SuperOverride{
86

9-
public void DisplayOverride()
10-
{
7+
class SubOverride extends SuperOverride{
8+
public void DisplayOverride() {
119
System.out.println("Sub Display.");
1210
}
1311
}
@@ -25,22 +23,21 @@ public static void main(String[] args) {
2523

2624
SuperOverride s3=new SuperOverride();
2725
s3.DisplayOverride();
28-
2926
}
3027
}
3128

3229
/*
3330
Explanation:
3431
Method Overriding:
3532
36-
The method DisplayOverride() is now public in both SuperOverride and SubOverride, which allows proper method overriding.
33+
The method DisplayOverride() is now public in both SuperOverride and SubOverride, which allow proper method overriding.
3734
Since the method signature remains the same, dynamic method dispatch (runtime polymorphism) occurs.
3835
3936
Dynamic Method Dispatch (Runtime Polymorphism):
4037
4138
The reference variable s is of type SuperOverride, but it is assigned an object of SubOverride (SuperOverride s = new SubOverride();).
42-
At runtime, Java determines that the actual object is of type SubOverride, so it calls DisplayOverride() of SubOverride, not SuperOverride.
39+
At runtime, Java determines that the actual object is of the type SubOverride, so it calls DisplayOverride() of SubOverride, not SuperOverride.
4340
4441
Correct Method Invocation:
4542
s.DisplayOverride(); correctly calls the overridden method in SubOverride.
46-
*/
43+
*/

0 commit comments

Comments
 (0)