0

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 
3
  • 2
    You are not showing us all your code, in particular not the line in which the error occurs. The error means you are creating a new Person object but you are not supplying the required parameters to the constructor. Commented May 25, 2014 at 9:38
  • 1
    By the way variables are usually written with a small letter in Java and usually in the constructor you should use this keyword Commented May 25, 2014 at 9:40
  • Show the code of Person Constructor arguments Commented May 25, 2014 at 9:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.