• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Calling an overridden method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Ranch Hand
Posts: 47
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just don't wanna be bewildered by OCA test creators

Your answer makes perfect sense. Thanks for that!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic