It looks confusing at first, but with a few very simple rules everything will be crystal clear.
Rule 1: every non-final inherited method can be overridden by a subclass (if the superclass is of course also non-final). Example: the
Object class has a
toString method (no parameters) and this method is overridden by the
SuperString class. And this
toString method is again overridden by the
SubString class as well.
Rule 2: you can overload any method by just changing the parameter list. So the
SuperString class can define a
toString method with 1
String parameter. Note: this method is also overridden by the
SubString class (so rule 1 is applied).
Rule 3 (a very important one): the type of the reference variable determines which methods can be invoked, which method is actually executed will be determined by the actual object the reference variable is refering to. Some examples to illustrate.
The statement from option (e)
is equivalent to
The
Object class doesn't define a
toString method with a
String parameter, so this code doesn't compile. Note: for the same reason the statement in option (f) won't compile either.
The statement frm option (d)
is equivalent to
The
Object class defines a
toString method without parameters, so the code compiles. The actual object is of type
SuperString, because the
SuperString class overrides the
toString method, the
toString method in the
SuperString class is executed, generating the desired output
Super String.
Hope it helps!
Kind regards,
Roel