1

I have a subclass extending a superclass. If constructor in super class has parameters a,b,c like MySuperClass(int a, string b, string c). And the constructor in the subclass has parameters a,d,e like MySubClass(int a, int d, int e), what should go inside the constructor of the subclass? Can I say super(a) so I don't have to duplicate the code for parameter a? but the constructor of super has 3 parameters; so I think I cannot do that.

Also, if I just ignore using super and assign the fields to parameters (like this.fieldName=parameterName) I would get the error that "there is no default constructor in super" why do I get this even though the super class has a constructor?

public abstract class Question {

    // The maximum mark that a user can get for a right answer to this question.
    protected double maxMark;

    // The question string for the question.
    protected String questionString;

    //  REQUIRES: maxMark must be >=0
    //  EFFECTS: constructs a question with given maximum mark and question statement
    public Question(double maxMark, String questionString) {
        assert (maxMark > 0);

        this.maxMark = maxMark;
        this.questionString = questionString;
    }
}

public class MultiplicationQuestion extends Question{

    // constructor
    // REQUIRES: maxMark >= 0
    // EFFECTS: constructs a multiplication question with the given maximum 
    //       mark and the factors of the multiplication.
    public MultiplicationQuestion(double maxMark, int factor1, int factor2){
        super(maxMark);
    }
}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Amir
  • 317
  • 5
  • 18

2 Answers2

2

The first thing a constructor always does is to call its superclass' constructor. Omitting the super call doesn't circumvent this - it's just syntactic sugar that saves you the hassle of specifying super() (i.e., calling the default constructor) explicitly.

What you could do is pass some default value to the superclass' constructor. E.g.:

public class SubClass {
    private int d;
    private int e;

    public SubClass(int a, int d, int e) {
        super(a, null, null);
        this.d = d;
        this.e = e;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

If constructor in super class has parameters a,b,c like MySuperClass(int a, string b, string c). And the constructor in the subclass has parameters a,d,e like MySubClass(int a, int d, int e), what should go inside the constructor of the subclass?

You are the only one to make this decision because it depends on what the numbers mean for your business case. As long as they are just numbers without any semantic meaning it does not matter.

Can I say super(a) so I don't have to duplicate the code for parameter a?

No, you have to specify which of the classes contructor parameters or constants should be passed to the constructor of the super class. Again there is no "automatic" solution. It is your responsibility as the programmer to decide which values are passed to the super class constructor and where they come from.

why do I get this even though the super class has a constructor?

the super classes constructor is not a default constructor (which has no parameters).

And how can I solve this issue?

Once more this has no general answer. Usually the only valid way is to provide values to pass to the super classes constructor. In very rare cases it might be suitable to create an additional default constructor.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51