This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling an overridden method

 
Lukasz Mazurek
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Below is a question that appeared while studying OCA Java 8.


Output:

In parent method | Child value
In child method | Child value



My question is:
How to call parent's version of ReturnString() in line 1, i.e. to get the response like ( I want to override ReturnString() method, not to hide it)

In parent method | Parent value
In child method | Child value



 
Rodion Gork
Ranch Hand
Posts: 47
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is: How to call parent's version of ReturnString() in line 1


Oh, it is a quite simple question. You just can't do this

If you think of it from ideological point of view, you may come to conclusion that you really never may want such behavior unless you want to bewilder people who are going to support your code.

Methods are declared as public or protected in order that they are linked dynamically (or virtually) - i.e. exactly to allow child method to "replace" them. I vaguely remember that languages like C++ allow to specify whether the binding should be virtual or not - but it is not the case in Java.

You can use "super" call inside the method of course... So technically you can end with solution where super method is either called or not depending on some flag. Also you perhaps can use invocation via reflection after finding the method you want. But I'm not sure there is any sane reason to do this.
 
Lukasz Mazurek
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just don't wanna be bewildered by OCA test creators

Your answer makes perfect sense. Thanks for that!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukasz Mazurek wrote:My question is: How to call parent's version of ReturnString() in line 1, i.e. to get the response like ( I want to override ReturnString() method, not to hide it)

If you really want to override the ReturnString() method, it's impossible to get your desired output. Simply because that's how polymorphism works.

But if you change the access modifier of the ReturnString() method in the superclass to private, you get exactly what you want Output:
In parent method | Parent value
In child method | Child value


In this topic you'll find excellent explanations about what's happening behind the scenes for both scenarios (using a similar code snippet).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic