2

Assume that I have some subclasses that extend a superclass. These subclasses differ by the parameters passed to the superclass. Unfortunately, like the following example, I can end up with "many" parameters. Is there a general method of avoiding this? Are constructors with "many" arguments considered good practice? Would it be better to have getter/setter methods instead of passing every parameter via constructor?

public abstract class SuperClass {
    private int a;
    private int b;
    .
    .
    private int z;

    public SuperClass(int a, int b, ... int z) {
        this.a = a;
        this.b = b;
        .
        .
        this.z = z;
    }
}

public class SubClass1 extends SuperClass {

    public SubClass1() {
        super(4, 3, ..., 9);
    }
}

public class SubClass2 extends SuperClass {

    public SubClass2() {
        super(1, 7, ..., 2);
    }
}
graviton
  • 465
  • 3
  • 13
  • 3
    You could try using an array `int[]`. – Christian Tapia Nov 26 '13 at 01:21
  • Without knowing the specific architecture of your application, pretty hard to say. In general, I avoid adding additional parroters to constructors past what is absolutely needed to get the function to run. – Giacomo1968 Nov 26 '13 at 01:23
  • Christian, sorry, I was using int as an example. I am meaning parameters that could vary in type. – graviton Nov 26 '13 at 01:24

5 Answers5

3

If your subclasses vary only in the parameters passed to the superclass, you might be looking for the Builder Pattern. A builder for the superclass lets you pass in whatever parameters you need without cluttering your constructor, and if you want subclasses for readability, you can just wrap a call to the builder and return its result from the subclass constructors.

Josh
  • 1,563
  • 11
  • 16
1

Generally, constructors with many parameters is a code smell. It means you probably have a class that breaks the "Single Responsibility Principle". If you can't avoid it, try using the builder pattern!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Johnny Z
  • 14,329
  • 4
  • 28
  • 35
0

I would not have constructors in my classes as the behavior of the objects instantiated by each constructor may be different and hard to determine.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
0

One thing to check is: should SuperClass be split into simpler classes?

If this can't be done: if you have too many parameters then you can have a special class that holds the parameters; with setters and getters for each parameter.

One can fill the values in from property files, so you can have profiles for common cases.

class SuperClassParam
{
  void seta(int a);
  int geta();

  //...
}

class SuperClass
{
  public SuperClass( SuperClassParam params )
  {
  }
MichaelMoser
  • 3,172
  • 1
  • 26
  • 26
-1

If the number of parameters can vary, then use a variable arity ("varargs") parameter. Declare an array instead of all those other instance variables. The variable arity parameter is typed as an array when in the method.

private int[] all;

public SuperClass(int... all) {
    this.all = all;
}

Your subclass constructors will not have to change at all.

rgettman
  • 176,041
  • 30
  • 275
  • 357