How to call super-version of a method that is overridden?

陌路散爱 提交于 2020-01-06 21:29:26

问题


I know how to call a method in a super class from a subclass by super. even when the method is overriden. But how can I call the method in the super class?

Is it impossible?

public class Test extends Super{

    public static void main (String[] args){
        Test t = new Test();
        t.print();
    }

    void print1(){
        System.out.println("Hello");
    }
}


class Super{

    void print1(){
        System.out.println("hi");
    }

    void print(){
        print1();
        // What can I do when I want to print "hi"
    }
}

回答1:


Once you override a function, the super-version of that function is no longer accessible, neither to the super class or the sub-class. It's called being "hidden". The only exception is calling it from within the overridden function, via super:

public class Test extends Super{
    void print1(){
        System.out.println("Hello");
        super.print1();                   //Here
    }
}
class Super{
    void print1(){
        System.out.println("hi");
    }   
}

(This is exactly the reason that constructors must never directly call functions that are potentially overridable.) You could also make the Super.print1() private, which would mean that, even though there is a Test.print1(), it does not override anything, because, as far as it's concerned, the super-version does not exist (is not visible).

So this:

public class Test extends Super{
   public static void main (String[] args){
      Test t = new Test();
      t.print();
   }
   void print1(){
      System.out.println("Hello");
   }
}
class Super{
   private void print1(){
      System.out.println("hi");
   }
   void print(){
      print1();
      this.print1();
   }
}

Outputs this:

hi
hi



回答2:


Just give your super class print1() method private visibility.

class Super{
    private void print1(){
       System.out.println("hi");
    }
    void print(){
        print1();
        // What can I do when I want to print "hi"
    }
}



回答3:


super.method_name(); 

That's how you call a method of the super class. If you want to do something different, please explain.




回答4:


When you override a method from a super class, it's mean you don't need to use the implementation of the super class. So you cannot do that. You could read more about Polymorphism. It could help.




回答5:


actually, if you want to call the method from the parent class, you shouldn't have overridden it.

the fact that you did override it, means that for each instance of Test, you want it to print "hello".

if you want "hi" to print, you have to call the print method through an instance of the parent class.




回答6:


create the object of Super and call print method

 Super s = new Super();
 s.print();

output:

hi



回答7:


If you definitely want to call Super.print1() from Super.print then you can make it private. Test.print1() will then not override it.




回答8:


Just use: super.method_name() from your method.



来源:https://stackoverflow.com/questions/24628276/how-to-call-super-version-of-a-method-that-is-overridden

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!