aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super of super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super of super" Watch "super of super" New topic
Author

super of super

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No way. "super." will invoke the nearest overriden method in the chain of parent classes.


SCJP2. Please Indent your code using UBB Code
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Try this.

Prints: D,C,B,A


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Here's another one.

Prints: C,SuperC,SuperC,SuperB,SuperA
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.)


"I'm not back." - Bill Harding, Twister
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
You are correct.

Prints: DDDD
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jul 02, 2002
Posts: 1865
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
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: super of super