inheritance problem OOP extend

Posted by hsmit on Stack Overflow See other posts from Stack Overflow or by hsmit
Published on 2010-06-06T10:08:44Z Indexed on 2010/06/06 10:12 UTC
Read the original article Hit count: 218

Filed under:
|
|
|
|

If a Father is a Parent and a Parent is a Person and a Person has a Father I create the following:

 class Person{
  Father father;
 }
 class Parent extends Person{}
 class Father extends Parent{}

Instances:

Person p1 = new Person();
Person p2 = new Person();
p1.father = p2; //father is of the type Father

This doesn't work... Now try casting::

Person p1 = new Person();
Person p2 = new Person();
p1.father = (Father)p2;

This doesn't work either.

What does work for this case?

© Stack Overflow or respective owner

Related posts about java

Related posts about oop