Firstly, could you read the code bellow.
1: class A{ int x;}
2: class B extends A{}
3: class Casting{
4: public static void main(
String args[]){
5: A objA = new A();
6: B objB = new B();
7:A temp = objB;
8:B test1 = (B)temp;
9:B test2 = (B)objA;
10:}
11:}
I understand that the line 9 will compile but will not run since objA previously is a parent class A - cannot cast a class which was not a subclass to subclass type. But why is line 8 possible to run?
I`ve figured that you can assign child class B to parent class A but not vice versa. And you can call parent method methodA from child object objB but not vice versa. But I dont understand the part "you cannot assign parent class to a child class." Please someone explain this to me!!