-2

Why we cannot inherit a class with the private constructor in Java? Can anyone explain with simple example?

Karan
  • 359
  • 4
  • 18
  • Possible duplicate of [extends of the class with private constructor](https://stackoverflow.com/questions/3952815/extends-of-the-class-with-private-constructor) – Frontear Jan 08 '19 at 19:28
  • You can extend a class with a private ctor: it just has to be defined in the same compilation unit. https://ideone.com/UXCpXr – Andy Turner Jan 08 '19 at 19:40
  • And just for the record: please dont forget about accepting an answer at some point. – GhostCat Jan 08 '19 at 20:01

2 Answers2

0

Any private thingy within a class is only accessible from within that class. To any other class, even subclasses, it is like that private thing doesn't exist.

So, when your base class has only a private constructor, the child class simply couldn't make any kind of call to a super constructor. Because there is no such constructor that is visible to the child class.

Like:

class Base {
  private Base() { }
  static Base newInstance() { return new Base(); }
}

class Child extends Base { ...

There is no valid way to write give Child a constructor. Because any constructor has to call a super constructor. Of course, if Base had a non-private constructor, then the derived class has some super() it can call.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • What is the reason child class constructor has to compulsory call parent class constructor? – Karan Jan 08 '19 at 19:58
  • 1
    The Java Language spec that says so. Think about it: a child object is also **always** a "base class object". It has all the fields from base, too. So, all these fields must be initialized. – GhostCat Jan 08 '19 at 20:01
0

If it has only private constructors, you can't call them from a subclass. A subclass is required to call one of the parent class's constructors, but it can't because they're not visible. Even if you don't explicitly call a parent class constructor there's still an implicit call to super(), which fails if the default constructor is private.

class Parent {
    private Parent() { }
    private Parent(int param) { }
}

class Child extends Parent {
    // Invalid: Implied call to super(), which is private.
    Child() { }

    // Invalid: Cannot call super(int) since it's private.
    Child() { super(42); }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Why is it required to call parent class constructor? – Karan Jan 08 '19 at 19:56
  • The same reason you can't create an object normally without calling a constructor. Constructors ensure objects are initialized correctly. If a subclass didn't call the parent's constructor the parent object might not be in a valid state. – John Kugelman Jan 08 '19 at 19:58
  • Why not make parent's private constructor available to its children then? – parsecer Mar 14 '19 at 18:05
  • They wouldn't be private, then; they'd be protected. Protected methods are part of a class's API contract and can't be changed without risking breakage of client code. Private methods can be changed, added, and deleted freely without any such worry. – John Kugelman Mar 14 '19 at 18:16