如何调用超类的超级方法

时间:2015-02-19 09:37:51

标签: polymorphism override core superclass super

class a
{
void show()
{
}
class b extends a
{
void show()
{
}
class c extends b
{
//how to call show method of class a
}
}

}

有谁知道如何使用超级关键字

从类c调用类a的方法

1 个答案:

答案 0 :(得分:1)

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

class B extends A {
    void show() {
        System.out.println("B");
    }
}

class C extends B {
    void show() {
        super.show();
    }
}

The above code will display "B" when class C object is invoked.
In your case, there can be 2 options:
1. C extends A - This will let super.show() in show method of class C display "A".
2. Add super.show() in show method of class B so that show method of class C displays both "B" and "A".

There is no way to call super class methods which are higher than level 1.