0

I try to create a constructor in my extended class in order to create objects in my main class. When i generate the default eclipse constructor, it shows all the fields of my superclass and when I try to create the object in my main, I need to put all the fields.

Here is my Person class:

public class Person {
private String id;
private String name;
private String gender;
private String country;
private String cities;
private String birthDate;
private String deathDate;
private List<String> aliases = new ArrayList<String>();
private List<String> tags = new ArrayList<String>();

public Person(String id, String name, String gender, String country, String cities, String birthDate,
        String deathDate, List<String> aliases, List<String> tags) {

    this.id = id;
    this.name = name;
    this.gender = gender;
    this.country = country;
    this.cities = cities;
    this.birthDate = birthDate;
    this.deathDate = deathDate;
    this.aliases = aliases;
    this.tags = tags;
}

And here is my extended one, the group class:

public class Group extends Person{
private String name;
private String country;
private String cities;
private String beginDate;
private String endDate;
private List<String> aliases = new ArrayList<String>();
private List<String> tags = new ArrayList<String>();
private List<Person> members = new ArrayList<Person>();

public Group(String id, String name, String gender, String country, String cities, String birthDate,
        String deathDate, List<String> aliases, List<String> tags, String name2, String country2, String cities2,
        String beginDate, String endDate, List<String> aliases2, List<String> tags2, List<Person> members) {
    super(id, name, gender, country, cities, birthDate, deathDate, aliases, tags);
    name = name2;
    country = country2;
    cities = cities2;
    this.beginDate = beginDate;
    this.endDate = endDate;
    aliases = aliases2;
    tags = tags2;
    this.members = members;
}

This is the constructor that gets generated (the huge one) When I try to create object on my main it requires all the fields (even the Person's class fields) So is there any way to create Group objects by filling the fields of my Group class only? Thanks in advance.

Nick Gr
  • 105
  • 10
  • While you can create your own constructor with whatever parameters you want, you must still call `super(...)` with all of the expected parameters for a `Person`. More generally, however, I'd suggest re-thinking this inheritance hierarchy. Do you really believe a `Group` Is-A `Person`? I'd suggest no. – KevinO Mar 28 '19 at 01:15
  • Related: [HAS-A, IS-A terminology in object oriented language](https://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language) – KevinO Mar 28 '19 at 01:18

2 Answers2

0
public class Group extends Person {

When you write Group extends Person that implies that a group is a type of person. But that's not right. A group contains people; it's not itself a person. Don't use inheritance, use membership. You already have List<Person> members, so really just get rid of the extends clause.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Yes, I already thought of that, but unfortunately I have to put Group and Person objects in the same ArrayList later on. I didn't like this inheritance too but it's needed according to my professor. I think you can't put 2 different kind of objects in the same ArrayList since it ruins the "diamond". – Nick Gr Mar 28 '19 at 01:19
  • 1
    An `ArrayList` can hold anything. – John Kugelman Mar 28 '19 at 01:24
0

I think you should be able to create a smaller constructer and writing null for the fields in the

super (...)

that are not defined

public Group(String id, String name, String gender) {
    super(id, name, gender, null, null, null, null, null, null);
}

like this

PS be aware of nullPointer exceptions in the super class

jonathan Heindl
  • 844
  • 7
  • 16