How to call an overridden method?

Discussion in 'Java' started by Aaron Fude, May 16, 2008.

  1. Aaron Fude

    Aaron Fude Guest

    Queston is in the comment. Thanks so much!

    public class A {
    void f() {
    System.out.println("A");
    }
    }

    publicclass B extends A {
    void f() {
    System.out.println("B");
    }
    }


    public static void main(String[] args) {
    B b = new B();

    // What's the syntax for calling A.f() on b?

    }
     
    Aaron Fude, May 16, 2008
    #1
    1. Advertising

  2. Aaron Fude

    Ajay Guest

    On May 16, 12:30 am, "Peter Duniho" <>
    wrote:
    > On Thu, 15 May 2008 21:10:14 -0700, Aaron Fude <> wrote:
    > > Queston is in the comment. Thanks so much!

    >
    > >  public class A {
    > >     void f() {
    > >       System.out.println("A");
    > >     }
    > >   }

    >
    > >   publicclass B extends A {
    > >     void f() {
    > >       System.out.println("B");
    > >     }
    > >   }

    >
    > >  public static void main(String[] args) {
    > >     B b = new B();

    >
    > >     // What's the syntax for calling A.f() on b?

    >
    > >   }

    >
    >  From the main() method?  None.  It would defeat the purpose of an  
    > overridden virtual method for the client of the class to be able to pick  
    > and choose which version of the method to use.
    >
    > If you want your class to expose that as part of its API, you could  
    > provide a separate method that calls "super.f()".  That would allow for  
    > the functionality without going behind class B's back.  However, IMHO an  
    > API that supports that is ill-advised.  It begs the question as to why the  
    > method is virtual in the first place.  You may want to consider making it  
    > "final" instead of adding a new method to call the base class version.
    >
    > Pete


    Aaron:

    Perhaps this is a design issue: Usually, there are better ways to do
    things than calling base class's overloaded method.

    What are you trying to do?
     
    Ajay, May 16, 2008
    #2
    1. Advertising

  3. Aaron Fude

    Roedy Green Guest

    Roedy Green, May 17, 2008
    #3
  4. Aaron Fude

    Arne Vajhøj Guest

    Roedy Green wrote:
    > On Thu, 15 May 2008 21:10:14 -0700 (PDT), Aaron Fude
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >> // What's the syntax for calling A.f() on b?

    > see http://mindprod.com/jgloss/super.html


    Why ?

    That page does not describe the problem in the question ?

    Arne
     
    Arne Vajhøj, May 17, 2008
    #4
  5. Java doesn't have the ability (AFAIK) to have non-virtual methods.
    So...
    A a = new B();
    B b = new B();
    a.f();
    b.f();

    Both print "B"

    In C++ you have to explictly say these are "virtual" to get that
    behavior.
    In Java it is implied and that's that.
    No casting can get around the fact that methods are virtual.

    Possible work arounds (sorta).
    1) Add the "final" to A's f() to prevent any subclass from overridding
    it.

    2) Add a new method to B called superF() or something that just calls
    super.f().
    In other words just give them different names.

    There may be a way to do this using the reflection API but I haven't
    seen it and I don't think its likely.

    Class pSuperClass = b.getClass().getSuperclass();
    A realA = pSuperClass.newInstance();
    realA.f();

    But thats insane and the exactly the same as just writing:
    A realA = new A();
    realA.f();
     
    David A. Redick, May 30, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patel
    Replies:
    6
    Views:
    40,711
    Randall R Schulz
    Oct 23, 2003
  2. Patel
    Replies:
    1
    Views:
    432
    Harald Hein
    Oct 23, 2003
  3. jeremito
    Replies:
    22
    Views:
    580
    Bruno Desthuilliers
    Jan 6, 2007
  4. Hermann Lichte

    Overridden method doesn't get invoked

    Hermann Lichte, Oct 27, 2006, in forum: C++
    Replies:
    1
    Views:
    334
    Kai-Uwe Bux
    Oct 27, 2006
  5. Replies:
    0
    Views:
    380
Loading...

Share This Page