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 2 of 2

Thread: call super method outside of 'this'

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

    Default call super method outside of 'this'

    Hi,
    i would like to call a method defined in an ancestor class from a descendant class but outside of the scope of 'this'. The code below shows clearly what i need. The line with "INVALID!" remark shows the prolematic point. Is that call possible in any way?
    Thanks.
    public class Base {
     private String name;
     
     public String getName() {
      return name; // it is for example only, it is very complex in real app
     }
     
    }
    public class Sub extends Base {
     private Sub previous; // implements an one way list
     
     @Override
     public String getName() {
      Sub sub = this;
      while (sub.previous!=null) sub = sub.previous; // name is stored only in the first object
      return sub.super.getName(); // INVALID!
     }
     
    }
    Last edited by helloworld922; January 29th, 2011 at 01:06 PM.


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

    Default Re: call super method outside of 'this'

    Why not return super.getName()? In this context there is no need for 'this'. If however, you want a reference to an object in another client and call the parent function rather than the inherited implementation, as far as I know you cannot...and I'd recommend re-evaluating the design should you find the need to do so.

Similar Threads

  1. Super Bowl Tickets
    By Jonathanoty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2011, 03:17 PM
  2. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  3. SUPER SIMPLE QUESTION!!!
    By Options in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 2nd, 2010, 09:21 PM
  4. Super Mario Frustration
    By Bryan in forum The Cafe
    Replies: 2
    Last Post: June 11th, 2010, 02:46 PM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM