When we say that the base class Base and its derived class Derived are type compatible , we refer to the fact that the Base reference can refer to an instance of Derived .
That is, Base b = new Derived();
Casting will be required for the oposite because types are not compatible with the type. Now this concept is not applicable to primitive types?
I mean it
short shortNumber = 10; int intNumber = shortNumber;
it seems to me the same thing (since no casting is required, and both short and int are integer types).
Therefore, overriding the method in the base class, why is it acceptable that the return type is either the same, or at least compatible with the return type of the base class, but it also does not apply to integral types?
For example. why is this unacceptable?
public class Person { public int getId(){ return 1; } } public class Employee extends Person { public short getId(){ return 0; } }
Cratylus
source share