supertype assignmet

Hi guys I have a question for you,

let A and B two classes and B inherit from A, st B <: A

and a is type A and b is type B

why a = b is legal and b = a not??

Thanks a lot

All the best
B contains everything that A has, plus some.

Therefore, B can be made into a A by discarding the "extra stuff".

But A cannot be made into a B because the compiler does not know how to initialize B's "extra stuff".

You may want to look up "Liskov Substitution Principle" or "LSP", a fundamental in polymordhism - in essence, LSP says that any subtype can take the place of its supertype, but not vice versa.
Topic archived. No new replies allowed.