-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh10_48_DynamicMethodDispatch.java
More file actions
72 lines (57 loc) · 2.51 KB
/
Ch10_48_DynamicMethodDispatch.java
File metadata and controls
72 lines (57 loc) · 2.51 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class car{
public void typeCar(){
System.out.println("ICE Cars");
}
public void engineOn(){
System.out.println("Turning Engine On...");
}
}
class electricCar extends car{
public void typeCar(){
System.out.println("Electric Cars");
}
public void motorOn(){
System.out.println("Turning Motor On...");
}
}
public class Ch10_48_DynamicMethodDispatch {
/* Dynamic Method Dispatch refers to Runtime Polymorphism that is
which method will run is decided in the runtime. */
public static void main(String[] args) {
System.out.println();
car obj1 = new car();
obj1.typeCar();
// Allowed
System.out.println("***");
electricCar obj2 = new electricCar();
obj2.typeCar();
// Allowed
System.out.println("***");
System.out.println();
// obj1.model1();
// What about this?
System.out.println("Now understanding Runtime Polymorphism");
car newObj = new electricCar();
// This means here , reference is of Superclass and object is from Subclass
// electricCar newObj2 = new car(); // vice-versa is not possible , and gives an error
/* For ease learn like, If an electric car is going on road we can refer it to car
but if , simple car is going we cannot call it an Electric Car. */
/* That is a reference can be taken from Superclass for making an object of Subclass,
but vice-versa is not possible. */
// What about running an overridden method?
newObj.typeCar();
// Ans - The class whose object is made is responsible for running it's overridden method.
// This is called Runtime Polymorphism.
System.out.println("***\n");
System.out.println("Understanding reference and Object");
/* Now since here we have referred out electric car to a simple car , we cannot say turn a car's motor on
instead we say turn an electric car's motor on. */
// On the same logic , the following syntax gives an error
// newObj.motorOn(); // Gives an error , because we referred it to as car whereas
newObj.engineOn(); // This would work
}
}
/* In a nutshell:
1. We can create an reference from Superclass and object of subclass but not vice-versa
2. If overridden method is present , the class whose object is made is dominating
3. But sole methods is run on reference basis , that is only Superclass's method is allowed to run. */