I have a class C. Class E extends it.
E e = new E();
C c = new C();
Why is
e = (E) c;
Upon further review
Casting an object does not change the object to the object being cast, but allows another class reference that is related to it by inheritance to refer to the object.
For example C extends E
. And they both have a method myName();
. If you say
E e = new C();
e.myName();
you are calling C myName()
method and if you also say
E e = new E();
C c = (C)e;
you just told the compiler that it should allow you refer to E
with C
reference type.