[英]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.
* 只需取消注释 childs doStuff() 中的 super.asd() 即可看到两个打印。 **/
**/
public static void main(String [] args){
Child c = new Child();
c.doStuff(); // call methods
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.