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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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:
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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?
Last edited by copeg; October 13th, 2011 at 02:22 PM.
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Ack, crossposting, not cool.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!