In Java, how do I call a base class's method from the overriding method in a derived class?

后端 未结 12 950
無奈伤痛
無奈伤痛 2020-11-27 02:50

I have two Java classes: B, which extends another class A, as follows :

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public          


        
12条回答
  •  广开言路
    2020-11-27 03:15

    super.MyMethod() should be called inside the MyMethod() of the class B. So it should be as follows

    class A {
        public void myMethod() { /* ... */ }
    }
    
    class B extends A {
        public void myMethod() { 
            super.MyMethod();
            /* Another code */ 
        }
    }
    

提交回复
热议问题