Reputation: 223
I have 2 subclasses: Staff, Student they belong to superclass Person.
Here is the code(tasks) which is given by my teacher:
public class Person
{
private String name;
private int yearOfBirth;
/**
* Create a person with given name and age.
*/
Person(String name, int yearOfBirth)
{
this.name = name;
this.yearOfBirth = yearOfBirth;
}
}
class Student extends Person
{
private String SID; // student ID number
/**
* Create a student with no parameters.
*/
Student()
{
//task.
}
}
public class Staff extends Person
{
private String roomNumber;
/**
* Construct a staff member with field values and no pamaeters.
*/
public Staff()
{
//task
}
}
I don't know what can I type in order to create an object without parameter. It always appears an error like: constructor Person in class Person cannot be applied to given types; required: java.lang.String,int;
I have checked online that there are 2 ways to solve the problem:
add a default value in the superclass: Person()//without parameter
.
In the subclass Student:
Student()
{
Person astudent = new Student() //I guess.
}
Student()
{
super("xxx")//I guess.
}
I don't know what to do. I an a starter in learning BlueJ. Hope anyone can help me. Thank you very much.
Upvotes: 21
Views: 161340
Reputation: 32468
Since your superclass Person
doesn't have a default constructor, in your subclasses (Student
and Staff
), you must call the superclass constructor as the first statement.
You should define your sub-class constructors like this:
Student() {
super("a_string_value", an_int_value);// You have to pass String and int values to superclass
}
Staff() {
super("a_string_value", an_int_value); // You have to pass String and int values to super class
}
Upvotes: 23
Reputation: 11
student should not extend person. bcoz, if we create obj for student, person’s constructor will be called automatically.
Upvotes: 1
Reputation: 94
If you want to create an object of child class (ie Staff and Student) without passing parameters then you can create an additional constructor without parameters in the parent class (ie Person class) as below.
public class Person
{
private String name;
private int yearOfBirth;
/**
* Create a person with given name and age.
*/
Person(String name, int yearOfBirth)
{
this.name = name;
this.yearOfBirth = yearOfBirth;
}
// additional constructor without parameter
Person(){
// add your code here
}
}
now below code will work without any error.
Staff stf = new Staff();
Student std = new Student();
Upvotes: 2
Reputation: 12880
Add super(NAME_IN_STRING_TYPE,YEAR_OF_BIRTH_IN_INT_TYPE);
as a first statement in your subclasse's constructor like
Student constructor
Student()
{
super("name", 1970); // String,int arguments passed
//task.
}
Staff constructor
Staff()
{
super("name", 1970); // String,int arguments passed
//task.
}
This is needed since there is no default no-arg constructor in the base class. You have to explicitly define a no-arg constructor in base class or you need to instruct the compiler to call the custom constructor of the base class.
Note : Compiler will not add default no-arg constructor in a class if it has a user defined constructor. It will add the default no-arg constructor only when there is no constructor defined in the class.
Upvotes: 3
Reputation: 6533
To construct instance of Student
you need to do actions neccesary to construct Person
first. There is only one way to construct Person
- two-arg constructor. That means you have to change Student
like:
public Student() {
super("someName", 1950); //first values came to my mind
}
Although you should be aware that Student
should behave exactly like Person
if treated as Person
, i.e. have age and name. So actually I'd recommend to change Student
constructor to include name and age there.
Upvotes: 2
Reputation: 2185
for constructor no param you should have two constructors like
public class Student {
Student(String name , int dateOfBirth)
{
super(name,dateOfBirth)
}
Student()
{
//task.
}
}
also same for other class
Upvotes: 1
Reputation: 145
Try this:
Student(String name, int yearOfBirth) {
super(name, yearOfBirth);
// task...
}
Reason: you dont have a default constructor at your superclass. So you have to call super()
at the first position in your subclass constructor.
Upvotes: 2
Reputation: 9437
the first thing a constructor will do, is call the constructor (with same arguments) of the super class. Person does not have a no-argument constructor, so, you must change your code in one of next two ways:
Student(String name, int yearOfBirth)
{
//task.
}
or
Student()
{
super("", 0);
//task.
}
and the same goes for Staff
Upvotes: 3