• 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

super of super

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java allows to use super keyword to access overridden memembers of immediate superclass. Is there anyway you can invoke an overridden method in further up in the hierarchy ( 2 or more level up)
Thanks
Barkat
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No way. "super." will invoke the nearest overriden method in the chain of parent classes.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.

Prints: D,C,B,A
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another one.

Prints: C,SuperC,SuperC,SuperB,SuperA
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But s1 isn't being overridden in these examples - it's being hidden. Since s1 is a field and not an instance method, there is no dynamic polymorphism associated with it - hence, we don't use the term "overriding" when a subclass contains a field of the same name - we call this "hiding" instead. The examples aren't really relevant to the question. Try replacing s1 with a method, and you'll see there's no way to access a method once it's been overridden twice or more. (Not using a reference to the descendant class anyway.)
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct.

Prints: DDDD
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank guys. I thought so. I could not find anything in Java language or Standard Java classes. I guess one could write a custom method:
callOverRiddenMethodFromThisClass(String method_name, Class ClassName)
and put in the top level class (may be customized Object class) of your application. So that every class will inherit it. This method will make use of super to go one level up and check if that class is the same as ClassName. If it is then it will execute method_name. Else it will go one level up and repeat the same. If Object class is reached then throw an exception.
What do you guys think?
In FoxPro, if I could have some function name in a string, I could say:
&string
and that will execute that function. Is there some thing like that in Java. Also how do extract the class name of context class at run time?
Thanks
Barkat
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I add above method in Object class without renaming the Object class. That is I do not want to say:
class MyObject extends Object {
callOverRiddenMethodFromThisClass() { }
because it will force me to inherite all my classes from MyObject....
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barkat Mardhani:

In FoxPro, if I could have some function name in a string, I could say:
&string
and that will execute that function. Is there some thing like that in Java.
Thanks
Barkat



Prints: A.m1
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, you don't want to use reflection this way. It's better to re-arrange (re-design, refactor) your classes and methods so that such calls are not needed.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Newman:
Generally, you don't want to use reflection this way. It's better to re-arrange (re-design, refactor) your classes and methods so that such calls are not needed.


I agree. Reflection defeats Object Orientation. It's use should be limited to applications such as an IDE that works with Java Beans. I should have posted a disclaimer with that example. My intent was simply to demonstrate that a mechanism exists that allows a method to be invoked by name using a string.
 
reply
    Bookmark Topic Watch Topic
  • New Topic