简体   繁体   中英

If I invoke a parent's method in parent class, does it invoke child class method with the same name

I want to know whether a child class calling parent method that invokes an overloaded method in the parent class, will invoke the overloaded method in the child class

class Parent {

    void doStuff() {

    }

    void asd() {
        doStuff();
    }
}

class Child extends Parent {
    void doStuff() {
        // implementation
    }
}

static void main(Args... args) {
    new Child().asd(); -> does this invoke the doStuff with the implementation or the empty doStuff in the parent class?
}
class Parent{
    void doStuff(){
       System.out.println("parent class");
    }
    void asd(){
    doStuff();
    }
 }
 class Child extends Parent(){

       @Override
       void doStuff(){
         //super.asd();
         System.out.println("child class");
    }
 }

/** * When you run the program you will see the two methods being called * one from the parent class and then the override method for child. * just uncomment the super.asd() in childs doStuff() to see both print. **/

public static void main(String [] args){
    Child c = new Child();
    c.doStuff();  // call methods
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM