Oleg Kuts
Oleg Kuts

Reputation: 829

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?

Upvotes: 5

Views: 2477

Answers (7)

AnkeyNigam
AnkeyNigam

Reputation: 2820

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.

Upvotes: 3

Erwin Smout
Erwin Smout

Reputation: 18408

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.

Upvotes: 1

Estimate
Estimate

Reputation: 1461

public class B extends A {

/*
* The default constructor B means
* public () B {super();}
*/
   public B() {
   }
}

So you should define a constructor which is accessible by the class B

  1. You can change the access modifier for parent Constructor
  2. Or else you can define other Constructor in parent class which is accessible by class B and call that constructor.

Upvotes: 0

Dima Maligin
Dima Maligin

Reputation: 1492

An instance of class B creates an instance of class A so B must invoke super(...) if A implements a non default constractor. So the constructor should be protected for B being able to call super(...)

Upvotes: 0

Danielson
Danielson

Reputation: 2696

Can work

class A {
    private A() {
    }
    public A(int i){
    }
}

public class B extends A {
    private A(){
        super(10);
    }
}

Or remove the private A(), defaults to public A(), unless you have another constructor

class A {

}

public class B extends A {
    private A(){
        super();
    }
}

Upvotes: 0

Raman Shrivastava
Raman Shrivastava

Reputation: 2953

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.

Upvotes: 1

NiziL
NiziL

Reputation: 5140

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.

Upvotes: 10

Related Questions