user2507240
user2507240

Reputation: 33

how to represent a hierarchy situation with OOP?

I am trying to get my head around OOP thinking when say I have your general example of a person class and then a class for family members family members share the same address but not all persons have the same address.

how do you make it so you can create families where members share the same address without having to enter the same address on each person, but that different families have different addresses?

I have seen examples where you define subclasses that inherit from the main class person but you have to hardcode each family as a class. Any views as to how to make this work in OOP? I might be looking at the problem in the wrong way...

Upvotes: 0

Views: 222

Answers (3)

Shailesh Suryawanshi
Shailesh Suryawanshi

Reputation: 1270

The first thing before defining relations between Classes is to check if they follow any of the IS-A or Has-A relationship.

In the question above, Definitely there are two entities. One is a Person and other is a Family

Now we can chalk out relationship's between them as follow.

  1. Person Is-A family => This does not sound right. There should be more than one person to form a family.

  2. Person Has-A family => This sounds right. But person can have more than one family. Considering husband can have his family and extended family of his in-laws.

  3. person Has-A list of families. => This sounds perfect.

  4. Family Is-A person => This does not sound right.

  5. Family Has-A person => This does sound right but family can have more than one person.

  6. Family Has-A list of persons => This sounds perfect.

So, in all the above relationship's relation 3 and 6 are perfect.

In OOPS, Has-A are composition kind of relationships. Whereas the Is-A relationships follows inheritance.

So your Classes will have definition as follows.

public class Family {
    String name;
    Family[] families;
}


public class Family{
    String name;
    Person[] persons;
}

This is basically a many to many relationship and form's two way graph like structure. Depending on your use-case you can reduce this relationship to one-to-many.

Whereas, if you consider Monkey IS-A Animal and So is Cow IS-A Animal, With these relationship's we can define Parent-child Inheritance hierarchy.


abstract class Animal {
    abstract void makeSound();
}



class Monkey extends Animal{
    void makeSound() {}
}


class Cow extends Animal{
    void makeSound() {}
}

There are no one-size answer on how to define the relationship's. It completely depends on the use-case.

Upvotes: 1

fahmiduldul
fahmiduldul

Reputation: 1130

I think this approach:

class Family(object):
    def __init__(self, name, address):
        self.members = []
        self.name = name
        self.address = address
    
    def addMember(self, member):
        self.members.append(member)
    
    def getAddress(self):
        return self.address


class Person(object):
    def __init__(self, name, family, address = None):
        self.name = name
        self.family = family
        self.address = address
        
        self.family.addMember(self)

    def getAddress(self):
        return self.address if self.address else self.family.getAddress()


if __name__ == "__main__":
    family = Family("family 1", "family 1 address")

    person1 = Person("person1", family)
    person2 = Person("person2", family, address="person 2 address")

    print(family.members)
    print(person1.getAddress()) # print "family 1 address"
    print(person2.getAddress()) # print "person 2 address"

you can set the person address to be None and if address is None then get the data from Family class.

Upvotes: 0

jgh99
jgh99

Reputation: 56

One way to do it is to have 2 classes: Family and Person. The Family class will have two main attribute:

  • a collection of Person (as array, list, etc)
  • address

The Person class will need to have a reference to the Family object in which it belonged into. So its attributes might be like:

  • name
  • date of birth
  • ...
  • reference to Family Object

Generally, to represent hiearchy with OOP, you can do something called composition, which is to have some objects as the attribute of another object.

Upvotes: 1

Related Questions