How do I call an overridden parent class method from a child class?

后端 未结 3 962
渐次进展
渐次进展 2020-12-07 02:17

If I have a subclass that has methods I\'ve overridden from the parent class, and under very specific situations I want to use the original methods, how do I call those meth

3条回答
  •  旧巷少年郎
    2020-12-07 02:22

    call super

    class A {
       int foo () { return 2; }
    }
    
    class B extends A {
    
       boolean someCondition;
    
       public B(boolean b) { someCondition = b; }
    
       int foo () { 
           if(someCondition) return super.foo();
           return 3;
       }
    }
    

提交回复
热议问题