I'm new to Java and I see that we cannot subclass a class that has its constructor as private
. However, I came across this code which creates a subclass whose super class' constructor is private.
In short:
This works:
class A {
static class B {
private B() {
out.println("Constructor of B");
}
}
static class C extends B {
C() {
out.println("Constructor of C");
}
}
}
While this doesn't:
class B {
private B() {
out.println("Constructor of B");
}
}
class C extends B {
C() {
out.println("Constructor of C"); // No default constructor available for super class
}
}
Can anyone please help me understand what's going on here?