Why can't reference to child Class object refer to the parent Class object?

22,555

Solution 1

Exactly because aChild is a superset of aParent's abilities. You can write:

class Fox : Animal

Because each Fox is an Animal. But the other way is not always true (not every Animal is a Fox).

Also it seems that you have your OOP mixed up. This is not a Parent-Child relationship, because there's no composition/trees involved. This is a Ancestor/Descendant inheritance relation.

Inheritance is "type of" not "contains". Hence it's Fox is a type of Animal, in your case it doesn't sound right -- "Child is a type of Parent" ? The naming of classes was the source of confusion ;).

class Animal {}
class Fox : Animal {}
class Fish : Animal {}

Animal a = new Fox(); // ok!
Animal b = new Fish(); // ok!
Fox f = b; // obviously no!

Solution 2

If it was valid, what would you expect when you read aChild.prop3? It is not defined on aParent.

Solution 3

class "Child" extends "Parent"

"child class object is inherently a parent class object"

 Child aChild = new Child();
 Parent aParent = new Parent();
 aParent = aChild;// is perfectly valid.
 aChild = aParent;// is not valid.

in a code segment like a normal assignment operation, the above is read from right to left. line 3 of the code segment reads - "aChild (a Child class object) is a Parent" (due to inheritence child class objects become superclass objects inherently) thus line no.3 is valid.

whereas in line no.4 it reads, "aParent (a Parent class object) is a child" (inheritence doesn't say that superclass objects will become child class objects. it says the opposite) thus line no.4 is invalid.

Solution 4

If I have a class, say

class A{
    getA(){

    }
}

class B extend A{
    getB(){

    }
}

Now class B knows two methods getA() and getB(). but class A knows only getA() method.

So, if we have class B obj = new class A(); we have made a mess , as it is valid for class B to reference methods getA() and getB() but only getA() is valid. That explains the issue.

This is my understanding of not allowing child class hold reference of parent class.

Solution 5

The Heap-Stack answer by AaronLS makes perfect technical sense.

References are store on stack while objects are store on heap. We can assign child object to parent type reference because child is type of parent and child object has reference for parent class. While parent is not of type child. Parent object doesn’t have reference to child so child reference can’t point to parent object.

This is the reason why we can cast decimal to int and int to decimal. But we can not cast parent-child both ways. Because parent has no clue about its children's references.

Int i = 5;
Decimal d = 5.5;

d = i;

or 

i = d;

Both are valid. But same is not the case with reference types which are stored on heap.

Share:
22,555
claws
Author by

claws

Updated on November 23, 2021

Comments

  • claws
    claws almost 2 years

    I was explaining OOP to my friend. I was unable to answer this question.

    I just escaped by saying, since OOP depicts the real world. In real world, parents can accommodate children but children cannot accommodate parents. same is the case in OOP.

    class Parent
    {
      int prop1;
      int prop2;
    }
    
    class Child : Parent // class Child extends Parent  (in case of Java Lang.)
    {
      int prop3;
      int prop4;
      
      public static void Main()
      {
         Child aChild = new Child();
         Parent aParent = new Parent();
         aParent = aChild;// is perfectly valid.
         aChild = aParent;// is not valid. Why??
     
      }
    }
    

    Why isn't this statement valid?

     aChild = aParent;// is not valid. Why??
    

    since aChild's members are superset of aParent's members. Then why can't aChild accommodate a parent.

  • Henk Holterman
    Henk Holterman almost 14 years
    Nice example but it would be a lot more instructive with a few lines of code: Animal a = Fox; Fish f = a;
  • Adeel Ansari
    Adeel Ansari almost 14 years
    Exactly, the best answer so far. +1
  • claws
    claws almost 14 years
    oops!! What I meant was ChildClass & ParentClass. But it took completely different meaning. it became real world, child & parent. I wont edit & change it now, because I got so many answers correcting this. I should have taken more concrete class names.