4
Reply

Why base class instance can not be stored in derived class reference? Or Why it is not possible to instantiate the base class instance in derived class variable?

Ajay Gandhi

Ajay Gandhi

9y
1.1k
0
Reply

    reference variable of parent class can point to object of the derived class but vice verse is not possible,because the derived class contains all members of parent class, but parent class not contain all member of child class, it make sense

    Just like the variable of a class can be initialize by using instance of the same class , Same as Parent class variable can be initialize by using it's child class child class instance to make it reference class_child Cc = new class_child(); class_parent Pc = Cc; in the above case Pc is a Parent class reference that is created by using child class instance so Both Pc and Cc will be consuming or Accessing the same Object memory. Even if the Parent reference is consuming it's child class object memory by using Parent reference we Can't call any child class member that is "Purely define under the child class".

    Expecting Architect level answer

    It breaks the concept of OOPS Inheritance .