Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Method Hiding doubt

 
Greenhorn
Posts: 17
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to understand the method hiding concept. In below code why is the methodCall() in the ChildClass not getting called? As the Method() is inherited from the ParentClass, it should be able to access the methodCall() in the ChildClass right?




Output:
Parent Method call
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because ChildClass#methodCall is static and ParentClass#Method actually calls this.methodCall().
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:It is because ChildClass#methodCall is static


Correct

and ParentClass#Method actually calls this.methodCall().


No, it calls ParentClass.methodCall(), not this.methodCall(). If it called this.methodCall() the output would be Child Method call, not Parent Method call.

The reason why it calls ParentClass.methodCall() is because you can't override static methods, you can only shadow them. Any call made to static methods uses the declared type (so in this case the enclosing class ParentClass), not the actual type as is the case for instance methods.
 
Sheriff
Posts: 16945
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, your perspective that a Parent class somehow "accesses" methods of a subclass is misleading and clouding your understanding of what's happening.

What's really happening is the result of what's called early binding (also known as static binding). The method in question is declared as a static method therefore it does not participate in polymorphism which is achieved via late binding (aka dynamic binding) which happens at run time. Calls to static methods are resolved at compile time. In this case, since the call to the static method occurs in the superclass and is not qualified by a reference of some type, the compiler will statically bind the call to the method in the superclass. If you add a direct call to the static methodCall() in the ChildClass.main() method, say after line 22, you'll see the output of line 16.
 
Junilu Lacar
Sheriff
Posts: 16945
286
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
Here are some changes to your code that illustrate the points made previously:

Output is:

Parent Method call
Child Method call
Child Method call
 
mooooooo ..... tiny ad ....
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic