'java constructor in class cannot be applied to given types
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.
}
- add a super() in the subclass:
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.
Solution 1:[1]
Since your super class Person
doesn't have a default constructor, in your sub classes (Student
and Staff
), you must call the super class 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 super class
}
Staff() {
super("a_string_value", an_int_value); // You have to pass String and int values to super class
}
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
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.
Solution 6:[6]
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
Solution 7:[7]
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();
Solution 8:[8]
student should not extend person. bcoz, if we create obj for student, person’s constructor will be called automatically.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | blalasaadri |
Solution 2 | Stultuske |
Solution 3 | |
Solution 4 | copakawusau |
Solution 5 | Alexey Malev |
Solution 6 | Macrosoft-Dev |
Solution 7 | Vikas Naik |
Solution 8 | sanjay ram |