why is the statement at line 15 giving compile-time error and the statement at line 14 giving no error ?
i am not able to see the logic.
OCPJP 6.0 93%
OCPJWCD 5.0 98%
posted 6 years ago
Because TestOperator inherits from (or extends) Testy, we can say a TestOperator is a Testy.
On line 14 you're assigning a TestOperator object to a Test reference variable. Since all TestOperators are also Testys, this is fine.
But on line 15, you're assigning a Testy object to a TestOperator variable. But not all Testys are TestOperators - TestOperator is just one type of a Testy. So the compiler doesn't think you can do that.
Think of it in terms of classes that are intuitively obvious. E.g.
All cats are animals, so the first statement is safe. Not all animals are cats, so the second isn't.