简体   繁体   中英

Java Inheritance super class method calling internally

public class InheritanceDemo {
    public static void main(String[] args) {
        new B("C");
        System.out.println(" ");
    }
}
class A{
    void top(String s) {
        System.out.println("HEllo A");
    }
    public A() {
    }
    public A(String s) {
        System.out.println("HEllo A constructor");
    }
}

class B extends A{
    public B(String s) {
        System.out.println("HEllo A");
    }
}

OutPut:

HEllo A

I would like to know without calling the superclass method how was it is calling internally

You have implicit call of super() on first row of constructor. Ie new B() calls A default constructor. The call chain of constructors is as follows: B -> A. The chain of execution is A -> B. If you want to use non-implicit constructor of A inside B just call super("your string") as first statement of B constructor.

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