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.

Results 1 to 13 of 13

Thread: Super Keyword

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Super Keyword

    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Super Keyword

    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!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Super Keyword

    Just a thought, as in how do you get it.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Super Keyword

    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!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Super Keyword

    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.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Super Keyword

    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!

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Super Keyword

    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.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Super Keyword

    Quote Originally Posted by copeg View Post
    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!

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Super Keyword

    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.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Super Keyword

    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:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Super Keyword

    oh I didnt know about that

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Super Keyword

    Quote Originally Posted by copeg View Post
    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?
    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!

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Super Keyword

    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!

Similar Threads

  1. please help super new to Java
    By R.Rivera in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 13th, 2011, 07:58 PM
  2. SUPER SIMPLE QUESTION!!!
    By Options in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 2nd, 2010, 09:21 PM
  3. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  4. keyword Extends
    By chronoz13 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2009, 07:30 AM
  5. How to highlight search keyword in text?
    By Mohd in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: February 1st, 2009, 06:35 AM

Tags for this Thread