-
March 28th, 2011, 09:34 PM
#1
Inheriting Constructors
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.
-
March 29th, 2011, 04:12 AM
#2
Re: Inheriting Constructors
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.
Last edited by Londbrok; March 29th, 2011 at 04:14 AM.
-
March 29th, 2011, 04:29 PM
#3
Re: Inheriting Constructors
So, have an abstract method for initializing the fields?
-
March 30th, 2011, 12:11 PM
#4
Re: Inheriting Constructors
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.
-
March 31st, 2011, 11:30 AM
#5
Re: Inheriting Constructors
Originally Posted by henryswanson
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.
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:
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
}
}
I can't be more specific that that without knowing why you feel an abstract constructor is necessary.
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|