Jasmine Ziz
Jasmine Ziz

Reputation: 57

Creating an array of other class.

I want to create an array of class Person in class Donator, but I got this error "error: constructor Person in class Person cannot be applied to given types;"

Did I miss any important code?

Here is my code.

Person.java

public class  Person
{
private String Name, Address, Gender, BloodType;
private int ICNumber;
private double Height, Weight;

//constructor
public Person(String n, String add, String gen, String bt, int ic, double h, double w)
{
    Name = n;
    Address = add;
    Gender = gen;
    BloodType = bt;
    ICNumber = ic;
    Height = h;
    Weight = w;
}


//abstract method
//abstract void printPerson();

} //close Person

Donator.java

public class Donator extends Person
{

private String donatorID;
private Person[] myDonator;
private int numberOfDonator;

//constructor
public Donator(String id, String d)
{
    donatorID = id;
    myDonator = new Person[2];
}

public String getDonatorID()
{
    return donatorID;
}
}//close Donator

Upvotes: 0

Views: 58

Answers (1)

jdiver
jdiver

Reputation: 2348

Since you are extending Person in Donator class, you should call super constructor in Donator class first.

//constructor
public Donator(String id, String d)
{
    // this is the Person constructor.
    super("some string", "some string", "some string", "some string", 1, 1, 1);

    donatorID = id;
    myDonator = new Person[2];
}

This is because, the java compiler tries to place the code in your constructor which will call base class's Default constructor, as we don't have the base class default constructor, we get the compilation error.

Upvotes: 2

Related Questions