varun
varun

Reputation: 492

how to call superclass parameterized constructor by subclass?

Why i am getting error in the starting of Employee constructor that cannot find symbol constuctor Person?

class Person {
    String name = "noname";

    Person(String nm) {
        name = nm;
    }
}

class Employee extends Person {
    String empID = "0000";

    Employee(String eid) {// error
        empID = eid;
    }
}

public class EmployeeTest {
    public static void main(String args[]) {
        Employee e1 = new Employee("4321");
        System.out.println(e1.empID);
    }
}

Upvotes: 0

Views: 4531

Answers (3)

Reimeus
Reimeus

Reputation: 159754

You need to call

super(name);

as the first statement of the constuctor Employee as the compiler will otherwise implicitly call the no-argument constructor for Person which doesnt exist

where name is an added argument to Employee

Employee(String eid, String name) {
    super(name);
    empID = eid;
}

Take a look at example 8.2-1 from the JLS which shows how a similar example fails in the absence of an explicit super method call.

Upvotes: 6

avismara
avismara

Reputation: 5149

You should do something like this to get your program to work:

class Person {
    String name = "noname";
    Person(String name) {
        this.name = name;
    }
}

class Employee extends Person {
    String empID = "0000";

    Employee(String empID , String name) {
        super(name);
        this.empID = empID;
    }
}

public class EmployeeTest {
    public static void main(String args[]) {
        Employee e1 = new Employee("4321" , "Ramesh");
        System.out.println(e1.empID);
        System.out.println(e1.name);
    }
} 

I have a couple of points to add.

  1. It is always a good habit to make your data member private. If you want to access those members outside the class, use getters and setters. ( getName(), setName() etc. )

  2. Your constructors ( Person() and Employee() ) HAVE to be defined if you want to creating an object without using parameterized constructor. NO default contstructor will be provided for you if you want to instantiate using a non-parameterized constructor. So do THIS whenever you are using a parameterized constructor as a good habit:

    class Person {
        private String name = "noname";
        Person() {}
        Person(String name) {
            this.name = name;
        }
        public String getName() {
            return this.name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    class Employee extends Person {
        private String empID = "0000";
        Employee() {}
        Employee(String empID,String name) {
            this.empID = empID;
        }
        public String getEmpID() {
            return this.empID;
        }
        public void setName(String empID) {
            this.empID = empID;
        }
    }
    

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

When you create an employee, you'll need to specify both a name and an employee id - since every employee is a person, and every person needs a name. The constructor for Employee should probably look like this.

public Employee(String eid, String name) {
    super(name);
    empID=eid;
}

The super line specifies how to call the superclass's constructor. It needs to be in there because there's no constructor for the superclass without parameters. A superclass constructor must be called, there's only one available constructor, and that constructor needs the name argument to be specified.

Upvotes: 2

Related Questions