Is there a way to specify in a base class that the derived class must create a constructor with certain parameters? Kind of like an abstract method, but it appears to be forbidden to create an abstract constructor.
Printable View
Is there a way to specify in a base class that the derived class must create a constructor with certain parameters? Kind of like an abstract method, but it appears to be forbidden to create an abstract constructor.
Frankly I do not understand to what use-case your train of thought is implying...
Code is code, while runtime, it is already totally in place. There is nothing left that is "abstract" at all.
Any extending class must implement least one of the constructors available in the super class. There is no requirement as such to implement all. Thus any extending class will inherently do what you ask "be restricted to specified set of constructors". This requirement to have a constructor as such behaves for all shits and giggles musch like an abstract method.
The constructor must be callable, so if you say.... would create the base class in package A, have the constructors you do not want the extending classes to implement (but still want them there) as package private and have the extending classes in a different package... only the public constructor options would be accessible.
Only protected constructors, factory methods that create instances using specific constructor and matching set of params. That way you can limit and manage which way instances of your class are actually created.
So, have an abstract method for initializing the fields?
What he is saying is that there is no way to enforce constructor creation by an extending class.
The 'proper' way to do it is to correctly document your Abstract class explaining that the super (params) constructor should ALWAYS be called by the base class when creating a new instance.
Anybody who extends this class should abide by this, if not they are breaking the 'contract' that you have stated for the parent class.
The whole point is that any constructor in an abstract class that is NOT created in the base derived classes are inaccessible, and thus serve no purpose. It's done this way for a reason: maybe one type of base class wants to use certain constructors, but another class wants to use them all.
The subclass must use a superclass constructor, so if you only provide constructors in the superclass with arguments you want to enforce, the derived classes must call one of those constructors. How they do it is not, strictly speaking, any concern of the superclass. For example, if a superclass defines only a constructor taking two arguments, then a derived class must call that constructor during instantiation, although it can do it from a no-argument constructor that provides default values for the superclass constructor:I can't be more specific that that without knowing why you feel an abstract constructor is necessary.Code:abstract class Foo {
private int x;
private String y;
public Foo(int x, String y) {
this.x = x;
this.y = y;
}
}
class Bar extends Foo {
public Bar(int x) { // any parameters
super(x, "unknown"); // but superclass ctor must be called
}
}
Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
A. Aho and J. Ullman