Skip to content

Commit ce9533f

Browse files
committed
feat: Add DynamicMethodDispatch example with method overriding and polymorphism
WHAT the code does: Defines Super with methods: - meth1(): prints "Super Meth1". - meth2(): prints "Super Meth2". Defines Sub extending Super: - Overrides meth2() to print "Sub Meth2". - Adds meth3() as a new method. Defines DynamicMethodDispatch with main(): - Creates a Super reference pointing to a Sub object. - Calls meth1() and meth2() on the reference, demonstrating polymorphism. WHY this matters: Demonstrates dynamic method dispatch (runtime polymorphism) in Java: - Method calls are resolved at runtime based on the actual object type, not the reference type. Illustrates method overriding with @OverRide for correctness and clarity. Shows how subclass-specific methods (like meth3()) cannot be called on a superclass reference. HOW it works: Super sup = new Sub(); sup.meth1() → resolved to Super.meth1 → prints "Super Meth1". sup.meth2() → resolved to Sub.meth2 → prints "Sub Meth2". sup.meth3() → not allowed, since reference type Super doesn’t define meth3(). If a Sub reference were used directly, meth3() would also be accessible. Tips and gotchas: Use @OverRide to catch accidental mistakes in overriding method signatures. Dynamic method dispatch applies only to overridden methods, not to fields (field access depends on reference type). System.err is used in Sub.meth2(); this prints to standard error, which may look different from System.out in some consoles. Polymorphism is powerful for designing flexible systems but can be misused if hierarchies grow unnecessarily deep. Use-cases: Educational demonstration of runtime polymorphism in Java. Foundation for learning abstraction and interface-based designs. Practical basis for frameworks where behavior is decided at runtime via object type. Short key: class-super-sub dynamic-method-dispatch polymorphism. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9f45134 commit ce9533f

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed
Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,87 @@
1-
class Super
2-
{
3-
public void meth1()
4-
{
1+
class Super {
2+
public void meth1() {
53
System.out.println("Super Meth1");
64
}
75

8-
public void meth2()
9-
{
6+
public void meth2() {
107
System.out.println("Super Meth2");
118
}
129
}
1310

14-
class Sub extends Super
15-
{
11+
class Sub extends Super {
1612
@Override
17-
public void meth2()
18-
{
13+
public void meth2() {
1914
System.err.println("Sub Meth2");
2015
}
2116

22-
public void meth3()
23-
{
17+
public void meth3() {
2418
System.out.println("Sub Meth3");
2519
}
2620
}
27-
public class DynamicMethodDispatch
28-
{
29-
public static void main(String[] args)
30-
{
21+
22+
public class DynamicMethodDispatch {
23+
public static void main(String[] args) {
3124
Super sup=new Sub();
3225

3326
sup.meth1();
3427
sup.meth2();
3528
/*sup.meth3();*/
3629

37-
/*
30+
/*
3831
Sub sub1=new Sub();
3932
sub1.meth1();
4033
sub1.meth2();
4134
sub1.meth3();
4235
*/
43-
44-
/*
45-
The line Sub sub1 = new Super(); attempts to assign an instance of Super to a reference variable of type Sub. This is not allowed because Super is the parent class of Sub, but Sub is the subclass of Super.
46-
In other words, a Super object cannot be assigned to a Sub reference, as it may not have all the methods or behavior that Sub has. The JVM does not know how to handle this assignment safely, so this will not compile.
47-
48-
Key Takeaway:
49-
You cannot assign an instance of the superclass (Super) to a reference of the subclass (Sub).
50-
You can assign an instance of the subclass (Sub) to a reference of the superclass (Super) because of upcasting (polymorphism).
51-
However, you cannot do the reverse (Super to Sub), as this would cause a downcasting error unless explicitly cast with proper checks.
52-
*/
5336
}
5437
}
5538

5639
/*
57-
the object sup is of type Super, but it refers to an instance of the Sub class. This is an example of dynamic method dispatch (or runtime polymorphism),
40+
The line Sub sub1 = new Super(); attempts to assign an instance of Supper to a reference variable of type Sub.
41+
42+
This is not allowed because Supper is the parent class of Sub, but Sub is the subclass of Supper.
43+
44+
In other words, a Super object cannot be assigned to a Subreference, as it may not have all the methods or behavior that
45+
Sub has.
46+
47+
The JVM does not know how to handle this assignment safely, so this will not compile.
48+
49+
Key Takeaway:
50+
You cannot assign an instance of the superclass (Super) to a reference of the subclass (Sub).
51+
You can assign an instance of the subclass (Sub) to a reference of the superclass (Super) because of upcasting (polymorphism).
52+
However, you cannot do the reverse (Super to Sub), as this would cause a down casting error unless explicitly cast with proper checks.
53+
54+
55+
the object sup is of type Super, but it refers to an instance of the Subclass.
56+
This is an example of dynamic method dispatch (or runtime polymorphism),
5857
where the actual method invoked depends on the object's actual type, not the reference type.
5958
6059
Here's what's happening in your code:
6160
sup.meth1();
6261
63-
Since sup is of type Super, the method meth1() is directly called from the Super class. So, it prints "Super Meth1".
62+
Since sup is of type Super, the method meth1() is directly called from the Super class.
63+
64+
So, it prints "Super Meth1".
6465
sup.meth2();
65-
Although the reference type is Super, the actual object is of type Sub. Java looks at the actual type of the object at runtime (which is Sub),
66-
and invokes the overridden version of meth2() from the Sub class. Thus, it prints "Sub Meth2".
66+
Although the reference type is Super, the actual object is of type Sub.
67+
68+
Java looks at the actual type of the object at runtime (which is Sub),
69+
and invokes the overridden version of meth2() from the Sub class.
70+
71+
Thus, it prints "Sub Meth2".
6772
sup.meth3();
68-
Even though sup refers to an object of type Sub, the reference type is still Super. The method meth3() is defined only in the Sub class and not in the Super class. Since Super doesn't have a method meth3(),
73+
Even though sup refers to an object of type Sub, the reference type is still Supper.
74+
75+
The method meth3() is defined only in the Subclass and not in the Super class. Since Supper doesn't have a method meth3(),
6976
the compiler will give an error when trying to compile the code.
7077
7178
Why does sup.meth3(); give an error?
72-
The reason for the error is that the reference variable sup is of type Super, and the Super class does not define the meth3() method. Therefore, the compiler cannot resolve meth3() for the sup reference,
73-
even though the actual object is of type Sub which has the meth3() method.
79+
The reason for the error is that the reference variable sup is of type Super, and the Super class does not define the meth3() method.
7480
81+
Therefore, the compiler cannot resolve meth3() for the sup reference,
82+
even though the actual object is of type Sub which has the meth3() method.
7583
76-
sub1.meth1() calls the method from the superclass Super because it is inherited.
77-
sub1.meth2() calls the overridden method from the subclass Sub due to dynamic method dispatch.
84+
sub1.meth1() calls the method from the superclass Supper because it is inherited.
85+
sub1.meth2() calls the overridden method from the subclass Subdue to dynamic method dispatch.
7886
sub1.meth3() calls the method from Sub, as it is defined in that class.
79-
80-
*/
87+
*/

0 commit comments

Comments
 (0)