As discussed in Java Class Inheritance
, since every sub-class object is also a super-class object, we can assign a sub-class object to a super-class reference. This is also called as downcasting
the object of type Drama can be assigned to the reference of the type Entertainment.
Movie m = new Movie();
e = m; // LINE A
Drama d = new Drama();
e = d; // LINE B
m = d; // LINE C - WON'T WORK, SINCE A DRAMA OBJECT IS NOT OF TYPE MOVIE.
Here we created an
reference, and also created a
reference and object. At
, we have assigned the
. Similarly, we have created a
reference and object and assigned the same to the
The assignment at
will not work, since we are assigning a reference of type
to a reference of type
is not a
the type of
are different), the assignment causes a compilation error.
Look at Assigning Super Class Reference To A Sub Class Reference In Java
which explains how the references of super-class type can be assigned to a sub-class reference.