Accessing super-class methods from super-class

Discussion in 'Java' started by fi3nd, Apr 8, 2010.

  1. fi3nd

    fi3nd

    Joined:
    Apr 8, 2010
    Messages:
    1
    Hello,

    consider this example (theoretically!!!):

    Code:
    class A {
        public void a() {
            b(); // calls B::b if 'this' is instance of B
    
            // silly workaround:
            ((B) this).help();
        }
        public void b() {
            System.out.println("A::b");
        }
    }
    
    class B extends A {
        @Override
        public void a() {
            super.a();
        }
        @Override
        public void b() {
            System.out.println("B::b");
        }
        public void help() {
            super.b();
        }
    }
    
    public class Test {
        public static void main(String args[]) {
            A a = new B();
            a.a();
        }
    }
    
    Of course, the output is:
    B::b
    A::b

    Is there any way to access A::b (as it would be in C++) from A.a in this case? I know that Java always uses dynamic binding etc., however, I'm interested if there exists better workaround than that above.

    best regards,
    Krzysztof Zmiek
    fi3nd, Apr 8, 2010
    #1
    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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,743
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    709
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    469
    Michele Simionato
    Jan 15, 2004
  4. Kenneth McDonald
    Replies:
    5
    Views:
    300
    Kenneth McDonald
    Sep 26, 2008
  5. Matthew Braid

    $class::SUPER as well as $instance::SUPER?

    Matthew Braid, Aug 6, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    300
    Matthew Braid
    Aug 9, 2004
Loading...

Share This Page