Mr.NeedHelpAlot
Mr.NeedHelpAlot

Reputation: 39

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?

Upvotes: 0

Views: 71

Answers (3)

Deep
Deep

Reputation: 152

Driver, Manager & Sorter are just special kind of service that any person is efficient with. It may be one or more. So, best way to declare interfaces for them and declare method for each without body. Now declare class which extends abstract class & implements one or more interfaces.

public abstract class Person {
    private String name;
    private int age;
    private String location;

    // Constructor (Must)
    public Person(String name, int age, String location) {
        this.name=name;
        this.age=age;
        this.location=location;
    }
}

public interface Driver {
    void isDriving(String name, int age, String location);
}

public interface Sorter {
    void isSorting(String name, int age, String location);
}

public interface Manager {
    void isManaging(String name, int age, String location);
}

public class Person1 extends Person implements Driver, Manager {
/*
    Properties, Constructors & Getters/Setters
*/
    @overrides
    public void isDriving(String name, int age, String location) {
    //  Driving Function Body
    }

    @overrides
    public void isManaging(String name, int age, String location) {
    //  Managing Function Body
    }    
}

Upvotes: 0

Cookie
Cookie

Reputation: 303

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.

Upvotes: 3

Silvio Mayolo
Silvio Mayolo

Reputation: 70317

You've discovered one of the fundamental limitations of inheritance: It creates a very tight coupling between parent and child. "A Sorter is a Person". Not "A Sorter is some other kind of Person", but literally "A Sorter is directly an extension of Person and nothing else".

You can use composition to make explicit who to call next in the "inheritance" chain.

public interface Person {
  public void reportSick();
}

public class OrdinaryPerson implements Person {
  public void reportSick() {
    // An "ordinary" person does nothing when they report sick.
  }
}

public class Driver implements Person {
  private Person next;

  public Driver(Person next) {
    this.next = next;
  }

  public void reportSick() {
    // Driver specific stuff goes here ...
    this.next.reportSick();
  }

}

// Then do the same for Sorter and Manager ...

Now, if a person is "just" a driver, you can represent them as

new Driver(new OrdinaryPerson())

This person is both a driver and an ordinary person (OrdinaryPerson is our null object; it has no actual behaviors to speak of, so we can use it to break our chain).

If a person is both a driver and a sorter, we can write

new Driver(new Sorter(new OrdinaryPerson()))

and assuming Driver and Sorter each call the inner reportSick method, they'll both get called when you call the method on this aggregate we've constructed.

Upvotes: 0

Related Questions