Skip to content
Advertisement

Object is more then 1 child, now what?

I have an abstract class called Person.

A person has multiple child classes e.g. Driver, Sorter, Manager. Each of which has its unique methods, and the ones shared e.g. name/age/location, etc and so I then thought that an abstract parent-class would be the solution. (Person)

Yet now I face a new problem, What if a person is both? Now I have an ‘Object’ which is a Driver but also a Sorter. So when he/she calls in sick, Person has a method called reportSick() But when object driver.reportSick() is being called, there is a chance that there is another object for the same Person that is a Sorter.

It looks strange to me to have 2 objects for the same thing (The person in this case)

What am I overlooking/doing wrong/misunderstanding?

Advertisement

Answer

You can go this way:

  • Make the interface(or abstract class if needed) Profession and implement your classes (Driver, Sorter, Manager) from it.

  • Make Person not abstract and add there field List<Profession>. And when you need some methods call it in cycle for each Profession.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement