0
class ClassA {}

class ClassB extends ClassA {}

class ClassC extends ClassA {}

and

ClassA p0 = new ClassA();
ClassB p1 = new ClassB();
ClassC p2 = new ClassC();
ClassA p3 = new ClassB();
ClassA p4 = new ClassC();

 p0 = p1 works
 But, p1 = p2 fails compilation....

Could not figure out why is this behavior when the hierarchy is same in both the statements? A --> B --> C

Sameer
  • 425
  • 1
  • 7
  • 15

1 Answers1

1

In your hierarchy A->B->C is not true. It is A->B, A->C. And C is not a subtype of B.

dbf
  • 6,399
  • 2
  • 38
  • 65