-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicMethod.java
55 lines (45 loc) · 1.18 KB
/
DynamicMethod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package learnJava;
//parent, super class
class A2
{
public void show()
{
System.out.println("in A2");
}
}
//Child, sub class
class B2 extends A2
{
public void show()
{
System.out.println("in B2");
}
public void config()
{
System.out.println("Config it is");
}
}
class C2 extends A2
{
public void show()
{
System.out.println("in C2");
}
}
public class DynamicMethod
{
//compile time and runtime
public static void main(String[] args)
{
A2 obj=new B2(); //this linking will be done at run time, which show method will be called will be decided at run time.(run time polymorphism)
//reference is from A2
//but the object which we are calling is from B2
//reference from superclass and object from sub class is possible
obj.show();
// obj.config(); we can't use this because A2 don't know anything about config method
//if we are using reference from super and object from sub we can call only those methods which are there in reference that is A2 here
obj=new C2(); //using the same reference of A2 and object from C2
obj.show(); //calling show from the object of C2
//every time we are changing the methods is known as dynamic Method Dispatch
}
}