Domanda

We were instructed to define the String "name" as private in the parent class Person. However, when I try to display the names of Person and Employee objects, the Employee names are printed as "null". I'm not allowed to define "name" as public.

Person class:

public class Person {

private String name;
public int weight, height;

public Person() {}

public Person(String name) {

    this.name = name;
}

public String getName() {

    return name;
}


public String toString() {

    return this.getName() + ", your height is " + height + " and your weight is "
            + weight;
}

Employee class:

public class Employee extends Person {

private int id;

public Employee(String name, int id) {
    this.id = id;
    this.name = name;
}

public int getID() {
    return id;
}

}

Main class:

Person a1 = new Person("Thomas");
    a1.height = 70;
    a1.weight = 160;
    Person a2 = new Employee("Rebecca", 6543);
    a2.height = 65;
    a2.weight = 128;
    Person a3 = new Employee("Janice", 8765);
    a3.height = 60;
    a3.weight = 120;
    Person[] arr = new Person[] {a1, a2, a3};
    Person a;
    for (int i = 0; i < arr.length; i++) {
        a = arr[i];
        System.out.println(a.toString() + "; your BMI is: " + a.calcBMI());
    }
    Person.countEmployees(arr);

Output:

Thomas, your height is 70 and your weight is 160; your BMI is: 22.955102040816328 null, your height is 65 and your weight is 128; your BMI is: 21.297988165680472 null, your height is 60 and your weight is 120; your BMI is: 23.433333333333334

I asked my professor via email and she said that because toString() is defined in the parent class Person, this error should not occur. Is my syntax wrong? Logic wrong?

È stato utile?

Soluzione 2

i am not sure how this class of yours is compiling

public class Employee extends Person {

  private int id;

  public Employee(String name, int id) {
      this.id = id;
      this.name = name;
  }
}

When you say this.name = name and the Person.name is private, this should not compile if you have not defined another variable called name in Employee class.

Back to your question, you need to call the Person's constructor in the Employee consructor, something like this

public class Employee extends Person {

  private int id;

  public Employee(String name, int id) {
      super(name);
      this.id = id;
  }
}

Altri suggerimenti

The problem is that your Employee constructor is calling the default Person constructor (the call is automatically injected by the compiler, since the default constructor is defined. You can remove that default constructor in Person, and you'll see that it doesn't compile).

You should be calling the constructor that receives the name in the Employee constructor, instead of setting the property (which shouldn't be compiling, btw)

public Employee(String name, int id) {
  super(name);
  this.id = id;
}

You should delete the line this.name = name; in the Employee constructor and add a call to super(name); as the first line of that same constructor.

It is because it is private at Person class, so you do not have access to it in your Employee class, therefore you cannot set it from there directly.

Change this

public Employee(String name, int id) {
    this.id = id;
    this.name = name;
}

To this, it calls the contructor you need to set name :

public Employee(String name, int id) {
    super(name);
    this.id = id;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top