Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Calling overridden parent method of outter class from an inner class

  1. #1
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Calling overridden parent method of outter class from an inner class

    Greetings,

    I have an outer class that, lets call it "Outter" extends FileHandler and overrides the flush method. In the Outter's flush method, I have an inner class from which I need to invoke the overridden FileHandler's flush method.

    I figured out one way to do this via reflection. Unfortunately, I can't use super.flush() because the super class of the inner class is Object, so that doesn't work.

    In Outter.flush(), I created a local Class variable:
    Java Code:
    final Class<?> sc = super.getClass();
    Then from the inner class' method I do:
    Java Code:
    Method fl = sc.getMethod("flush");				
    fl.invoke(sc, (Object[])null);
    I'm wondering if there isn't a better way that avoids the use of reflection.

    Thanks,
    John

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Calling overridden parent method of outter class from an inner class

    Did you try the following syntax (in a method of the Inner class)?

    Java Code:
    Outer.this.flush();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Calling overridden parent method of outter class from an inner class

    Java Code:
    Outter.super.flush();
    Looks like a bad design to me, though.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Calling overridden parent method of outter class from an inner class

    Jos was faster, which is usual nowadays (but did rather seem to miss the point :P)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Calling overridden parent method of outter class from an inner class

    Quote Originally Posted by DarrylBurke View Post
    Jos was faster, which is usual nowadays (but did rather seem to miss the point :P)
    What point?

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Calling overridden parent method of outter class from an inner class

    Thank you both, I was JUST about to post a reply asking if Outter.super.flush() would work. It wasn't clear to me that the parent class' flush method would have been invoked using the instance of Outter from which it is being called.

    Darryl, I agree completely with your observation; this looks hokey as hell. That is because I have eliminated all detail that explains why I'm trying to call the flush method of the parent class.

    The bigger picture is this is part of an enterprise application in which file repositories owned by the server subject are being employed. Threads running under the caller subject won't have access to the files. In this particular case, I'm using "Outter" to extend FileHandler to ensure actions that manipulate a repository file do so as the server subject instead of as the caller subject.

    In this particular case, I'm focusing on overriding the FileHandler's flush method such that I use Subject.doAs to call the parent class' flush method (the one I really want) as the server subject. To accomplish the doAs, I am instantiating an anonymous instance of PrivilegedExceptionAction, whose run() method needs to invoke FileHandler's flush() for the current instance of Outter; and that's where trying to call the super class' method from an inner class is coming from in this case.

    Again, thank you very much for your replies.
    John
    The outter class' flush method in this example will use Subject.doAs to perform the flush operation under the server's subject instead of that of the caller. The desired flush operation is nothing more than calling FileHandler's overriden flush method (i.e. the outter class' parent's method)Therefore, the PrivilegedExceptionAction that doAs will perform as the server subject needs to call outter's parent's flush method (that of FileHandler)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Calling overridden parent method of outter class from an inner class

    Quote Originally Posted by JosAH View Post
    What point?

    kindest regards,

    Jos ;-)
    super, not this.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Calling overridden parent method of outter class from an inner class

    I think this would be a (very slightly) cleaner approach: create a separate method, say superFlush(), in the subclass Outter, that invokes super.flush(). Invoke that method from the method-local inner class.

    db
    jlczuk likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Calling overridden parent method of outter class from an inner class

    That's an interesting idea, I will try that. Thanks

Similar Threads

  1. Main Method Can't Find It's Parent Class
    By Slinkie45 in forum New To Java
    Replies: 11
    Last Post: 05-09-2011, 03:37 AM
  2. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  4. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 02:12 AM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •