Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- From: Daniel Pitts <newsgroup.spamfilter@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 28 Jul 2008 14:39:29 -0700
Tom Anderson wrote:
On Sun, 27 Jul 2008, Peter Duniho wrote:Yes it is, why not use:
On Sun, 27 Jul 2008 18:17:04 -0700, Mark_Galeck <mark_galeck_spam_magnet@xxxxxxxxx> wrote:
Hello, what is the RIGHT THING, to write a constructor for a
subclass, that needs to compute arguments to the super() call?? How
do experienced and elegant Java programmers do it?
If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to super() from your constructor. For example:
public class TestDerived extends TestBase
{
public TestDerived(int i)
{
super(intFromArg(i));
}
public static int intFromArg(int i) { return i; }
}
The static method can be as complicated as needed, leaving the call to super() with just the method call rather than the actual computation.
That's what i would say is the Right Way to do it.
The problem comes when you have multiple arguments that need to be computed together. For instance (and note that this isn't supposed to be an example of good design!):
public class Customer {
public Customer(String firstName, String lastName) {
// do whatever
}
}
public class SlashSeparatedCustomer extends Customer {
public SlashSeparatedCustomer(String name) {
// THIS IS ILLEGAL!
int slash = name.indexOf('/') ;
String first = name.substring(0, slash) ;
String last = name.substring(slash + 1) ;
super(first, last) ;
}
}
The SlashSeparatedCustomer constructor as it is is illegal, but there's no way to refactor it to use a static method, since you can't use a single method call to fill in two arguments in a call. Well, you could do this:
public class SlashSeparatedCustomer extends Customer {
public SlashSeparatedCustomer(String name) {
super(splitName(name)[0], (splitName(name)[1]) ;
}
private static String[] splitName(String name) {
int slash = name.indexOf('/') ;
String first = name.substring(0, slash) ;
String last = name.substring(slash + 1) ;
return new String{} {first, last} ;
}
}
But that's grotesque.
public class SlashSeparatedCustomer extends Customer {
public SlashSeparatedCustomer(String name) {
super(firstName(name), (lastName(name)) ;
}
private static String firstName(String name) {
return name.substring(0, name.indexOf('/')) ;
}
private static String lastName(String name) {
return name.substring(name.indexOf('/')+1) ;
}
}
Actually, why is this a separate class at all? It just takes a different argument type for the constructor, sounds like a static factory method is what you want.
This is a bad design for many reasons, including the fact that you're calling a non-final/non-private member method from the constructor.
In this case, what i would do is refactor the superclass constructor so that it does its work by calling some method, let's call it init, and then provide a protected no-args constructor:
public class Customer {
public Customer(String firstName, String lastName) {
init(firstName, lastName) ;
}
protected void init(String firstName, String lastName) {
// do whatever
}
protected Customer() {
// DOES NOT INITIALISE THE OBJECT!
}
}
Then you can write the subclass like this:
public class SlashSeparatedCustomer extends Customer {
public SlashSeparatedCustomer(String name) {
super() ; // could leave this implicit
int slash = name.indexOf('/') ;
String first = name.substring(0, slash) ;
String last = name.substring(slash + 1) ;
init(first, last) ;
}
}
The problem with this, of course, is that it means that Customer doesn't guarantee that it always constructs itself properly, and thus if a subclass gets it wrong and fails to call init, you have a broken instance.
whoah whoah whoah! Don't try to handle initialization after the fact.
You could have a flag, private boolean initialised, [snip]
I'm not really sure why java has the rule that the super call must be the first thing in the constructor.Because Java guarantees that the super class is fully constructed before the construction of the derived class begins.
I understand that you can't let a constructor call instance methods, or access instance fields, until that's been done, but why not let it call static methods, and methods of other classes, and manipulate local variables? Basically, the rule would be that you can't make explicit or implicit use of 'this' before the super call. In that case, the first, currently illegal, version of the SlashSeparatedCustomer constructor would be allowed, and thus life would be a lot simpler.
tom
It sounds like you're trying to create a new class that simply handles initialization differently than the base class. if that is ALL you are doing, then consider using a factory method instead:
public class Customer {
public static Customer fromSlashSeparatedName(String name) {
int slash = name.indexOf('/') ;
String first = name.substring(0, slash) ;
String last = name.substring(slash + 1) ;
return new Customer(first, last);
}
//... rest of Customer class
}
If you *really* do need implementation *and* interface inheritance, then still consider using a factory method.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.
- Follow-Ups:
- Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- From: Patricia Shanahan
- Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- References:
- what is the RIGHT THING to make a constructor of a subclass, using super()
- From: Mark_Galeck
- Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- From: Peter Duniho
- Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- From: Tom Anderson
- what is the RIGHT THING to make a constructor of a subclass, using super()
- Prev by Date: Re: how to compare two version strings in java
- Next by Date: Re: OutOfMemoryError with javamail getmessage
- Previous by thread: Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- Next by thread: Re: what is the RIGHT THING to make a constructor of a subclass, using super()
- Index(es):
Relevant Pages
|
Loading