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
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.)
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
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....
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
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.
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.
Post by:autobot
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