I have a constructor in an abstract class that takes many parameters in it's constructor
I am trying to extend this class and I want to set some default values in the call to super() in the extended constructor.
The problem is that by looking at the code it looks messy seeing:
super(0,1,2,3,4,5,6,7,8,9);
(bearing in mind this is simplified for stack overflow).
What I want to do is have the constructor like this:
public Test(){
int a = 0;
int b = 1;
int c = 2;
int d = 3;
int e = 4;
int f = 5;
int g = 6;
int h = 7;
int i = 8;
int j = 9;
super(a,b,c,d,e,f,g,h,i,j);
}
Purely for visual and ease of reading purposes. Is there any way to do this?
No, you can't do that - but if the important part is to give the arguments meaningful names, you could consider something like:
class Foo extends Bar {
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = 10;
public Foo() {
super(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}
Personally I'd find that more self-explanatory than having local variables within the constructor anyway.
Another option for explanation is comments:
public Foo() {
super(
10, // width
10); // height
}
That's only going to be the same number of lines as your "declare separate variables" code anyway.
See more on this question at Stackoverflow