raja singh kumar wrote:
My question is on method hiding. I understand class methods are hidden not overridden and the method that is called in the case of method hiding is the one in the super class. But I am confused about the terminology.
raja singh kumar wrote:
The Cat class overrides the instance method in Animal and hides the static method in Animal.
But the output is in conflict with the above statement. The static method in Animal is not hidden. Infact it is the one which is invoked because we are using Animal class to invoke the class method. What is the italicized line trying to say?
Henry Wong wrote:
raja singh kumar wrote:
My question is on method hiding. I understand class methods are hidden not overridden and the method that is called in the case of method hiding is the one in the super class. But I am confused about the terminology.
The method that is called, when methods are hidden, depends on the type of the reference that is used to call the method. It is not necessary the "one in the super class" that is called.
Henry
Mike London wrote:
I'm not sure if there is any direct way to "upcast", say, a Student reference in this case to call a parent method.
Junilu Lacar wrote:
Mike London wrote:
I'm not sure if there is any direct way to "upcast", say, a Student reference in this case to call a parent method.
Language mechanics and other technical details aside, if you feel a legitimate need to call the method in the superclass instead of the subclass, that to me indicates some kind of design flaw. Seems like a lot of learners get caught up with questions like "How can I do this in Java" and totally ignore the more important question of "Why would I even want to do this in Java?" It's like asking "How do I jump up and kick myself in the back of the head?"... my first response would be to say, "Why do you even want to do that?" not "Well, maybe you can get up on a bridge that's high enough..."
You should start getting suspicious whenever you are casting anything. Look at the following:-That shows there is a bit of dubious design in List implementations because some have a method to ensure they have the capacity to add many instances and some implementations don't have such a method. A linked list, for example, always has a capacity exactly the same size as its contents. The fact that two casts are required should make people suspicious about that sort of design. It should also make people suspicious about my spelling. Whether you are casting “down” or “up”, both should count as a code smell.Mike London wrote:I'm not sure if there is any direct way to "upcast", say, a Student reference in this case to call a parent method.
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|