You cannot create an abstract constructor, but a workaround you can do:
Deploy the abstract class with all the constructors you want to implement. In each of them, you will have one statement that references the abstract method of this class, the implementation of which will be in a subclass. Thus, you force the user to provide the body for the constructor by implementing an abstract method. For instance:
public abstract class MyAbstractClass { public MyAbstractClass(int param) { method1(param); } public MyAbstractClass(int param, int anotherParam) { method2(param, anotherParam); } protected abstract void method1(int param); protected abstract void method2(int param, int anotherParam); }
In the implementation, you will be forced to provide an implementation of these two methods, which can represent the bodies of two constructors:
public class MyClass extends MyAbstractClass { public MyClass(int param) { super(param); } public MyClass(int param, int anotherParam) { super(param, anotherParam); } public void method1(int param) {
source share