5
votes

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?

7
(The answer to your final question is "yes", provided you mean by "final" that "it can't be subclassed". It should (probably, I think) not be assumed however that the compiler will also spot this and treat class A exactly the same as if it had explicitly been declared "final".) - Erwin Smout

7 Answers

10
votes

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.

3
votes

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.

1
votes

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.

1
votes

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.

0
votes

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();
    }
}
0
votes

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

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