Forums Register Login

super of super

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
No way. "super." will invoke the nearest overriden method in the chain of parent classes.
+Pie Number of slices to send: Send
Try this.

Prints: D,C,B,A
+Pie Number of slices to send: Send
Here's another one.

Prints: C,SuperC,SuperC,SuperB,SuperA
+Pie Number of slices to send: Send
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.)
+Pie Number of slices to send: Send
You are correct.

Prints: DDDD
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 994 times.
Similar Threads
can one access private methods of a superclass with a sub class reference?
True or false
Constructor Question
Default access variables
question from master exam
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:46:49.