10

Suppose that we have a class a:

class A {
  A._();
}

Its default constructor is private: A._().

Is there any way to extends that class?

Problem

class B extends A {

}

This results in a compiler error:

The superclass 'A' doesn't have a zero argument constructor.

Trying to compose any constructor for B myself (B()) results in another error:

The superclass 'A' doesn't have an unnamed constructor.
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • If you write a constructor, the class does not get a default constructor (the implicit `A():super();` constructor that would be added otherwise). The `A._` constructor is not a "default constructor", nor is it an unnamed constructor. You can still add an unnamed constructor `A();` to the class as well. – lrn Jun 27 '18 at 06:18
  • @lrn *Default constructor* was the term I found for it. Usually it would be `A()`, but that gets removed once `A._()` is added. Thus `A` does neither have a *public* zero argument nor unnamed constructor. `A()` would be zero argument und unnamed. – creativecreatorormaybenot Jun 27 '18 at 07:07

1 Answers1

10

No, there is no way. This is an effective way to prevent extending.

What you still can do is implementing the class.

class B implements A {}

If the class also has a public non-factory constructor, you can still extend it by forwarding the constructor call to such a named constructor of the super class.

class B extends A {
  B() : super.other();
}
lrn
  • 64,680
  • 7
  • 105
  • 121
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    That is awful. What reason would there be to prevent extending? I noticed that implementing was possible, but that is not what I am looking for. Also, I will just adjust my question with *PascalCase*. I wrote those error messages myself. I did not really name classes *`a`* and *`b`*. – creativecreatorormaybenot Jun 26 '18 at 08:11
  • Most serious programming languages allow that https://stackoverflow.com/questions/10170633/c-sharp-sealed-vs-java-final. There are many things a class can't guarantee when it can be extended. – Günter Zöchbauer Jun 26 '18 at 08:13
  • Extending would only add features, or am I wrong? If the class is not intended to be extended, there are no overrides necessary anyway and one could just add to the class what they want. I also do not understand why privacy is enforced by the compiler. Ignoring it would be my own responsibility. – creativecreatorormaybenot Jun 26 '18 at 08:18
  • 1
    Extending can either add features or broke it. :) Depends on the implementations. – Yosi Pramajaya Jun 26 '18 at 08:21
  • 1
    "Ignoring it would be my own responsibility". If a developer doesn't want his classes to be used in ways that would prevent them from working correctly, he should have the power. If this prevents you from using a package, then raise an issue to make the author aware. Not everyone wants a JavaScript-like world where everyone can do what he wants and then swamp the internet with complaints and bug reports and buggy code ;-) – Günter Zöchbauer Jun 26 '18 at 08:25