Search code examples
javainheritancesubclasssuperclasssuper

Can I pass multiple Super calls in a constructor?


Hi I am basically getting an error message saying,

constructor Member in class Member cannot be applied to given types;
required: java.lang.String,java.lang.String; found:java.lang.String;
reason: actual and formal argument lists differ in length 

But I am not sure why, I thought maybe it could be cause I am not allowed to pass multiple super calls in the constructor? would this be correct?

This is my super class, which hold name and email

public class Member
{
    // The teacher's or Student's name.
    private String name;
    // The teacher's or Student's email;
    private String email;

    /**
     * Constructor for objects of class Member
     */
    public Member(String name,String emailID)
    {
        this.name = name; 
        email = emailID;
    }  

}

and this is constructor for the subclass, which I get the error on when I try to compile.

public Student(String name, String emailID)
    {
        super(name);
        super(emailID);
        attendance = 0;
    }

From my understanding, this should work fine but it is not, could anyone shed some light as to why this is not working?

Thanks


Solution

  • It can't work because super class needs two parameter, but you just provide them one by one, so first constructor super(name); can't compile, that's why such an error exists. So you should pass them together like this :

     super(name, emailID);
    

    Edit: Also, you can only call the super constructor once and it should the first thing to call.