-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpcastingDowncasting.java
More file actions
34 lines (28 loc) · 904 Bytes
/
Copy pathUpcastingDowncasting.java
File metadata and controls
34 lines (28 loc) · 904 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
class A {
public void show1() {
System.out.println("In a Show");
}
}
class B extends A {
public void show2() {
System.out.println("In b Show");
}
}
public class UpcastingDowncasting {
public static void main(String args[]) {
// double d = 4.5;
// // int i = d; // 4.5 can not be stored, but 4 can be stored, java will not
// allow
// // it but we can say its okay by doing typecasting
// int i = (int) d;
// System.out.println(i);
A obj = new A();
obj.show1();
A obj1 = (A) new B(); // upcasting - not compulsary, that is how dynamic method dispatch happens
// internally
obj1.show1();
// obj1.show2(); // but how do we call show2 method
B obj2 = (B) obj1; // downcasting
obj2.show2();
}
}