Skip to content

Commit d546a2b

Browse files
Create Reference _Variable_Casting.java
reference variable casting
1 parent bbbd2a8 commit d546a2b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Reference _Variable_Casting.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main;
2+
/*
3+
@author:Akash Tiwari
4+
*/
5+
6+
class A{
7+
8+
}
9+
class B extends A{
10+
11+
}
12+
public class consrtuct {
13+
14+
public static void main(String[] args) {
15+
A a=new A();
16+
B b=new B();
17+
// b=a; //Type mismatch: cannot convert from A to B
18+
19+
//But this can also be overcome by making the behaviour of A to be as B.
20+
a=b; //line20
21+
b=(B)a; //This will not cause any compilation error
22+
/*
23+
The order of line 20 and 21 is importent as it first make
24+
a polymorphic polymorphism is made so that the next line make itself as pure polymorphism
25+
else it gives classCastException runtime error
26+
*/
27+
System.out.println("Success");
28+
}
29+
30+
}

0 commit comments

Comments
 (0)