调用超类的重写方法

时间:2014-02-03 20:07:16

标签: java

这个问题在接受我的采访时被问到了。我的子类中有一个重写方法。使用子类的实例,我想调用超类的方法。请记住该方法已被覆盖。

以下代码的输出为NO NO PRINT HI 如果我想使用覆盖类的对象打印Print Hello怎么办?我该怎么做?

class OverrideSuper {
    public void printHello() {
         System.out.println("Print Hello");
    }
}

public class Overriding extends OverrideSuper {
    public void printHello() {
         System.out.println("NO NO PRINT HI");
    }

    public static void main(String[] args) {
        //OverrideSuper obj1 = new OverrideSuper();
        Overriding obj2 = new Overriding();
        obj2.printHello();// this calls printHello() of class Overriding.
        //I want to call printHello() of OverrideSuper using obj2. How do I do that???
    }
}

3 个答案:

答案 0 :(得分:4)

我认为面试官希望你写另一种叫super.printHello()的方法:

public class Overriding extends OverrideSuper {
    public void printHello(){
         System.out.println("NO NO PRINT HI");
    }
    public void superHello() { // You can make this private or protected, too
        super.printHello();
    }
    public static void main(String[] args) {
        //OverrideSuper obj1 = new OverrideSuper();
        Overriding obj2 = new Overriding();
        obj2.superHello();// this calls printHello() of class OverrideSuper .
    }
}

基本上,他希望你告诉他,一旦方法被覆盖,就无法从外部调用基类的方法。绕过它的唯一方法是在覆盖类的内部创建一个超类的调用路径,这就是我上面通过定义superHello方法所做的。

  

[采访者]说“我不相信”

另一种可能性是面试官正在寻找您提供基于反射的解决方案,这种解决方案只能在Java 7+中使用。您需要使用MethodHandle,使用基类调用findSpecial,然后调用您返回的方法。

Here is a link to an answer that explains the process, and shows an example.

答案 1 :(得分:0)

我认为这将满足您的要求!!试试这个 当您致电obj2.printHello();时,执行将移至Overriding s printHello(),并且在该方法中我使用了super.printHello();这意味着首先调用超类的printHello() {{1}然后再次执行来and compiler prints Print Hello s OverridingprintHello()

print NO NO PRINT HI

<强>输出

class OverrideSuper {
    public void printHello() {
        System.out.println("Print Hello");
    }
}
    public class Overriding extends OverrideSuper {
            public void printHello() {
                super.printHello();
                System.out.println("NO NO PRINT HI");
            }

            public static void main(String[] args) {
                // OverrideSuper obj1 = new OverrideSuper();
                Overriding obj2 = new Overriding();
                obj2.printHello();// this calls printHello() of class Overriding.
                // I want to call printHello() of OverrideSuper using obj2. How do I do
                // that???
            }
        }

答案 2 :(得分:-2)

您可以使用super关键字访问父方法。

class OverrideSuper{
    public void printHello(){
         System.out.println("Print Hello");
    }
}
public class Overriding extends OverrideSuper {
    public void printHello(){
        super.printHello();
        //System.out.println("NO NO PRINT HI");
    }
    public static void main(String[] args) {
        //OverrideSuper obj1 = new OverrideSuper();
        Overriding obj2 = new Overriding();
        obj2.printHello();// this calls printHello() of class Overriding.
        //I want to call printHello() of OverrideSuper using obj2. How do I do that???
    }
}