• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion regarding method hiding

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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 doc says:

The Cat class overrides the instance method in Animal and hides the static method in Animal.

Shouldn't the statement be that the Animal class hides the static method in Cat and not the other way round as mentioned in the italicized statement above?

The example given in the oracle documentation is given below.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya.I got it. The version of class method is invoked depends on the Class used to invoke the method. But looking at the example code used in the 1st post,  the accompanying line says.

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
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



The term "hiding" (along with "overriding", "shadowing", and "obscuring") are defined by the Java Language Specification. Personally, I think (especially the last one) that it is a bit of overkill, but oh well...  ... it is also, like everything else in the JLS, a bit overly complex ...

To try to simplify it a bit ... In terms of the difference between "hiding" and "overriding", with "overriding", the method in the subclass is always chosen. There is no way to access the method of the super class. With "hiding", it is ... well ... just hiding. There is a set of rules that will pick one over the other (hence, hide one from the other), but you can still access both, if you qualify the type (by casting, using a specific reference variable, etc.).

Henry
 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



You can easily downcast from a polymorphic instance if needed.

Example:



OUTPUT >>>

Employee Name: John Doe
Employee Salary: $123.34
----------------------------
Student Name: Phil the Groundhog
Student Salary: $100.0
Student ID: 123456

----------

- mike

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..."
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..."



LOL, I wasn't saying I would, or that you should (agree with your post); instead, I was just listing the sometimes-asked question I hear from my students.

I just removed that last sentence entirely.

Thanks!

- mike
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

You shoul‍d 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 shou‍ld make people suspicious about that sort of design. It shou‍ld also make people suspicious about my spelling. Whether you are casting “down” or “up”, both shou‍ld count as a code smell.
If a Student object has been given a particular form of the method foo(), then you either call that method or you call nothing.Line 3 will call Student#foo; that bit shou‍ld be obvious. If the Person class has a foo() method, then that will be registered at compile‑time, but at runtime, the method will be called from inside the Class object, so the Student#foo() (overriding version) will be called following the normal rules of polymorphism. You are calling the method depending what type of object you have. The only way you can get at the superclass' version of the method is from inside the overriding method with the construct super.foo();
The compiler won't be happy about line 7 because Object doesn't have a foo() method. The cast is valid, but the method call isn't, so that line will fail to compile. Line 9 will compile because a student might be a research student; you can hold your hand on your heart and say that a research student IS‑A student. But the cast will throw an exception at runtime.

The line I quoted raises an interesting point (), and I think you oughtn't to have removed it from its original location.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think in practice I've ever needed to downcast the way I demonstrated.

That downcast example, however, or something like it, came from some Java book, somewhere, a long while back. I never suggested it was good practice.

The Vector example was the OP's, not mine, as you know.

I'll be glad to put the line of text back in there I removed for a ... cow.  

Moo!

Thanks for your replies.

- mike
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic