简体   繁体   中英

java constructor accessing (if we have private constructor in child class)

I have a class A and class B which extends Class A.In class AI have a constructor "A" and in class BI have a constructor "B" and I have declare constructor "b" as private. Will it be able to access its super class constructor "A"?

class A {
    public A(){
    }
}
class B extend A{
    private B(){
    }
}

Yes, B constructor will be able to call constructor of A.

class A {
    public A(){
    System.out.println("Hello A constructor");
    }
}
class B extends A{
    private B(){
    super();
    System.out.println("Hello B constructor");
    }
    public static void main(String args[])
    {
    new B();
    }
}

Output - Hello A constructor Hello 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