21

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:

  1. add a default value in the superclass: Person()//without parameter.

    In the subclass Student:

Student()
  {
  Person astudent = new Student() //I guess.
  }
  1. 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.

1
  • Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Commented May 27, 2014 at 13:03

8 Answers 8

23

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Right, But why java compiler behave this way? When it is creating empty constructor when no provided, Why it isn't creating when having parameter?
3

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

Comments

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.

Comments

2

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.

Comments

2

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.

Comments

2

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();

Comments

1

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

Comments

1

student should not extend person. bcoz, if we create obj for student, person’s constructor will be called automatically.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.