File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super of super

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No way. "super." will invoke the nearest overriden method in the chain of parent classes.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.

Prints: D,C,B,A
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another one.

Prints: C,SuperC,SuperC,SuperB,SuperA
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct.

Prints: DDDD
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic