Hi Guys,
I wanted to know how to access the methods of grandparent class from the child class (when those methods in child class are overridden) using super keyword. Please help.
Printable View
Hi Guys,
I wanted to know how to access the methods of grandparent class from the child class (when those methods in child class are overridden) using super keyword. Please help.
Why do you think you need to do this? Sounds like a symptom of bad design to me.
Just a thought, as in how do you get it.
I'm not convinced you can, and I'm not convinced you should. Like I said, it's more likely a symptom of a bad design.
I get what you are saying. But as I was attending a Java training class, a student came up with this question, so we were asked to find an answer for the same. As 'super' is used to get the methods of parent class, how can super be manipulated to get the methods of grandparent class.
Well, let me know what you come up with. I don't see an obvious way, other than adding methods in the parent class that can call methods in the grandparent class.
As Kevin mentioned, this is poor design and a situation that should be avoided at all costs - if something is designed this way, its time to redesign. That being said, you could use reflection to accomplish this, but its an ugly solution to an already ugly problem that may not always work.
I've been trying to figure out how to do this with reflection, to no avail! Reflection doesn't exactly fit into my brain though, so that's probably my ignorance showing.
I tried this:
Code java:public class InnerClassTest { public static void main(String... args){ new ClassC().print(); } } class ClassA{ public void print(){ System.out.println("A"); } } class ClassB extends ClassA{ public void print(){ System.out.println("B"); } } class ClassC extends ClassB{ public void print(){ try { ((Class<ClassC>)this.getClass().getSuperclass().getSuperclass()).getMethod("print").invoke(this); } catch (Exception e) { e.printStackTrace(); } } }
But that just ends up calling ClassC's print() method, which results in a StackOverflowException, which is what I would expect. Hmph.
I think its a lot uglier than that. You need to get the grandparent Class, create a new instance of said class, - and this is where it gets ugly real fast - set all the appropriate values of the newly constructed grandparent object (assuming the classes are 'bean'-like, with getters and setters, you use the child values to set the grandparent values), then finally invoke the method. Did I mention this is ugly? ;)
This thread has been cross posted here:http://www.java-forums.org/new-java/49849-access-methods-grandparent-class.html
Although cross posting is allowed, for everyone's benefit, please read:
oh I didnt know about that
Wow, nice. You weren't lying about the ugliness though- that's assuming that the grandparent class's "print" method doesn't involve other methods that the grandchild class overrode (probably not from the OP's description, but it does make it even uglier for slightly more complicated scenarios). I think I just threw up in my mouth a little.
Ack, crossposting, not cool.