forked from akash-coded/core-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicPolymorphism.java
More file actions
43 lines (34 loc) · 969 Bytes
/
DynamicPolymorphism.java
File metadata and controls
43 lines (34 loc) · 969 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.lang.reflect.*;
class ParentClass {
public void belongsToParentClass() {
System.out.println("Unique to ParentClass class");
}
public void method() {
System.out.println("In ParentClass");
}
}
class ChildClass extends ParentClass {
public void belongsToChildClass() {
System.out.println("Unique to ChildClass class");
}
@Override
public void method() {
System.out.println("In ChildClass");
}
}
public class DynamicPolymorphism {
public static void main(String... args) {
ParentClass a = new ParentClass();
ParentClass b = new ChildClass();
a.method();
b.method(); // Dynamic method dispatch
a.belongsToParentClass();
b.belongsToParentClass();
// b.belongsToChildClass();
ChildClass obj = (ChildClass) b;
// b.belongsToChildClass();
int x = 5;
double y = x;
int z = (int) y;
}
}