Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a private base class constructor result in "Implicit super constructor is not visible"

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?

like image 699
Oleg Kuts Avatar asked Jul 03 '15 11:07

Oleg Kuts


People also ask

What happens if you declare a constructor as private?

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.

What is implicit super constructor?

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.

Why can't this () and super () both be used together in a constructor?

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.

What happens if super class does not have default constructor?

-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.


4 Answers

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.

like image 80
NiziL Avatar answered Oct 19 '22 21:10

NiziL


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.

like image 27
AnkeyNigam Avatar answered Oct 19 '22 21:10

AnkeyNigam


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.

like image 28
Raman Shrivastava Avatar answered Oct 19 '22 21:10

Raman Shrivastava


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.

like image 21
Erwin Smout Avatar answered Oct 19 '22 22:10

Erwin Smout