Suppose that we have a class a
:
class A {
A._();
}
Its default constructor is private: A._()
.
Is there any way to extends
that class?
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With