If Parent
is parent class Which inherits Employee
class then why
Person p = new Person();
Employee e1 = (Employee)p;
is not a compile time error? should not compiler figure it out at compile time.
If Parent
is parent class Which inherits Employee
class then why
Person p = new Person();
Employee e1 = (Employee)p;
is not a compile time error? should not compiler figure it out at compile time.
Because it could be valid also. For example:
Person p = new Employee();
Employee e1 = (Employee)p;
Why would you expect compiler to produce error?
To make it more clear, lets assume casting from base class to derived class is not allowed. How'll you do the following then?
object o = new Employee();
Employee e1 = (Employee)o;
If compiler prevents you from casting at compile time you can't unbox any boxed struct, etc which is definitely needed.