Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 466
claws Avatar asked Jan 27 '10 09:01

claws


People also ask

Can children reference parent object class?

In Java, the reference variable of the Parent class is capable to hold its object reference as well as its child object reference.

Can we create parent object with child reference in Java?

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.)

Is a reference variable that is used to refer parent class object?

The super keyword in Java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Can we store superclass parent object in a subclass child reference variable?

Thinking vice versa, since a parent class object does not have all the methods and properties needed by a child class, a child class object cannot refer to a parent class object.


2 Answers

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!
like image 175
Kornel Kisielewicz Avatar answered Sep 23 '22 00:09

Kornel Kisielewicz


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

like image 44
Thomas Lötzer Avatar answered Sep 22 '22 00:09

Thomas Lötzer