Reputation: 119
I'm working on creating two classes for a sort of useless class project. The classes are Employee and Doctor, with Doctor extending Employee. Seems pretty simple, right? I thought so too.
Here's my code for Employee (excluding the header):
public class Employee {
protected String name;
public Employee(String n) {
name = n;
}
}
Here is my code for Doctor (excluding the header):
public class Doctor extends Employee {
protected String school;
public Doctor(String n, String s) {
name = n;
school = s;
}
}
This should work, right? Alas, when I try to compile the Doctor class (The Employee class compiles fine), BlueJ says "constructor Employee in class Employee cannot be applied to the given types; required: java.lang.String found: no arguments reason: actual and formal argument lists differ in length".
I know I'm probably doing something wrong here, but I have no idea what it is. Again, it could be just that I'm using BlueJ; I haven't tried compiling it with any other IDE of with cmd... yet... Any ideas on what I'm doing wrong?
Upvotes: 1
Views: 1998
Reputation: 5459
You need to add a super(n);
to your Doctor constructor.
In Java, constructors aren't automatically chained like that. Before it gets to your Doctor constructor it looks for a parameterless constructor of the base class, unless the first statement in your Doctor constructor is a call to super().
Upvotes: 4