-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_method_dispatch.java
More file actions
29 lines (27 loc) · 942 Bytes
/
Dynamic_method_dispatch.java
File metadata and controls
29 lines (27 loc) · 942 Bytes
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
//dynamic method dispatch: in this we can create an object of the subclass with referrence of the super class but not
// the other way round. When we call methods for this obbject, only overrided methods can be called and methods of superclass!
class Chocolate{
void type(){
System.out.println("im made from coco and milk");
}
void state(){
System.out.println("I am solid and melts easily");
}
}
class jelly extends Chocolate{
void type(){
System.out.println("im made from gelatin");
}
void state2(){
System.out.println("I am partially solid and translucent");
}
}
public class Dynamic_method_dispatch{
public static void main(String[] args){
Chocolate obj = new jelly(); //allowed
// jelly obj2 = new Chocolate(); //not allowed
obj.type(); //allowed
obj.state(); //allowed
// obj.state2(); //not allowed
}
}