How can I achieve this without the final
keyword? What must I change to the constructors?
public final class testName {
testName() {
//do something
}
}
You can use final keyword in java, sealed in C# to make a class non-extendable.
In Java, we use the final keyword to prevent some classes from being extended.
You cannot extend a final class.
If you make all your Constructors private
, then the class will also no longer be extendable.
public class TestName {
private TestName(){do something}
}
To see why, check section 3.4.4.1, 'The default constructor'. By declaring your private default constructor, the last sentence of the paragraph holds:
Such a [private] constructor can never be invoked from outside of the class, but it prevents the automatic insertion of the default constructor.
So effectively by declaring a constructor in the superclass that is not accessible, there is no (other) constructor that your subclass could call and thus Java prevents compilation.
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