If constructors do not inherits in Java, why do I get compile error(Implicit super constructor A() is not visible for default constructor. Must define an explicit constructor)?
class A {
private A() {
}
}
public class B extends A {
}
UPD. I know that super()
is called in implicit B constructor. But i don't get why it can't access private constructor with super()
. So, if we have only private constructors, class de facto is final
?
If a constructor is declared as private, then its objects are only accessible from within the declared class. You cannot access its objects from outside the constructor class.
Implicit Constructor Chaining A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.
If we include “this()” or “super()” inside the constructor, it must be the first statement inside it. “this()” and “super()” cannot be used inside the same constructor, as both cannot be executed at once (both cannot be the first statement). “this” can be passed as an argument in the method and constructor calls.
-If a superclass does not have a default constructor and does not have a no=arg constructor, the class that inherits from it must call one of the constructors that the superclass does have.
if B extends A
, B
must have an access to an A
constructor.
Keep in mind that a constructor always call super()
.
So here, the implicit parameterless constructor of B
can't call the A
constructor.
In Java, if you create an instance of Child Class
, Parent Class
instance gets created implicitly.
So Child Class constructor should be visible to Child Class
as it calls the constructor of Parent Class
constructor using super()
in the first statement itself.
So if you change the constructor of the Parent Class
to private
, Child Class
could not access it and could not create any instance of its own, so compiler on the first hand just does not allow this at all.
But if you want to private
default constructor in Parent Class
, then you need to explicitly create an overloaded public
constructor in the Parent Class
& then in Child class
constructor you need to call using super(param)
the public overloaded constructor of Parent Class
.
Moreover, you might think then what's the use of private
constructors. private
constructors is mostly used when you don't want others from any external class to call new()
on your class. So in that case, we provide some getter()
method to provide out class's object
.In that method you can create/use existing Object of your class & return it from that method.
Eg. Calendar cal = Calendar.getInstance();
. This actually forms the basis of Singleton
design pattern.
The important point is to understand that the first line of any constructor is to call the super constructor. The compiler makes your code shorter by inserting super() under the covers, if you do not invoke a super constructor yourself.
Now if you make that constructor in superclass private, above mentioned concept will fail.
Your class B has a default constructor, B() - because you didn't explicitly define any other one. That constructor implicitly calls its super constructor, A(). But you have explicitly made that one private to class A, so you have explicitly declared that no other class, including B, can have access to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With