-1

2 classes, A and B. class B inherits from class A. What's wrong with the following code?

B b3 = (A)(new B());

Why isn't it a legal Upcasting?

It make sense. B instructor creates the B part of the whole object then A create its part and then it acts like A.

Edmond
  • 43
  • 1
  • 10

1 Answers1

0

Simple:

This is not legal because a B must be a B, not an A. That cast to A "invalidates" that assignment.

GhostCat
  • 137,827
  • 25
  • 176
  • 248