I have a statement. I know the ups and downs of both Composition and Inheritance, but I am unable to realize that what would be the best option the given scenario.


A Software company is developing a website for a university using a multiple inheritance between classes. The classes include Person, Teacher, Student, GradStudent, GradTeachAsst.

For some reasons, the project design team decided not to use multiple inheritance for GradTeachAsst class, and give two alternatives. One is to use Composition and other is combination of Single Inheritance with Composition. Being a software developer which option you will select? Justify your answer with good reasons.



In my opinion, the best scenario would be to chose the Composition. But I am not getting the reasons in my mind. Can anyone explain me the solid reasons?

  • what would you compose and with what and how ? Why wiukd it be better than composition and single inheritance ? – Christophe Jul 27 '17 at 16:50
  • @Christophe that's what the problem statement states, the actual web is not being developed. it is just a statement to imagine this scenario in mind and tell what would be the better option. – Khubaib Khawar Jul 27 '17 at 16:51
  • 1
    To make inheritance tree decisions using only the names of classes, would mean following cargo cult design techniques: these techniques allow you to make decisions without using detailed information, but the decisions are invariably a poor match to the specific situation. – Frank Hileman Jul 27 '17 at 17:46
  • 1
    I think your supposed to simply reproduce the pros and cons of each from the text book – Ewan Jul 27 '17 at 18:12
  • 1
    This sure seems like homework. I am sure the right academic answer can be found in the text or from this: en.wikipedia.org/wiki/Composition_over_inheritance – Thomas Carlisle Jul 27 '17 at 19:02

As you're pointing out, the inheritance solution requires introduction of hierarchy that serves very little value. In particular the use of multiple inheritance because someone is both a teacher and a grad student, is IMHO overly complex. What if the teacher is not a grad student, but a regular student — that requires yet another class; implying that in a more substantial domain the result would be somewhat of an explosion of classes.

Approaching the model from the persistence perspective, such inheritance complicates persistence, again while offering little or no value. The persistence model is probably simple and easily done using composition. So, why not follow that in object model as well.

Here I'm speaking broadly in support of using composition for Student with Person as well as Teacher with Person, not just for Grad Student Teacher. With composition there is no need for additional classes to model a person that has a teaching role as well as taking courses. To be clear, it would be odd to choose inheritance for Student and Teacher, but switch to composition for GradTeachAssist; while it would be natural to use composition for all of them.

And yet from logical perspective, fundamentally, we are (is a) person, but the roles we play, e.g. as student or teacher are ephemeral; these are not permanent is-a relationships, thus composition seems more appropriate.

  • Seems a good reason, but is it relevant to the infrastructure of Programming? I think in the statement it is asked to provide a logical reason, that is a realistic reason I think which you gave. – Khubaib Khawar Jul 27 '17 at 17:03
  • yeah I understand what you are talking about. But let it be the way as it is in the Model, what if we just have to answer about the GradTeachAsst and not all of the Model that is given? – Khubaib Khawar Jul 27 '17 at 17:05
  • 1
    I advocate refactoring the whole thing using composition; I say the model as you're showing it, is basically broken. "Is-a" is a stretch for the relationship between student and person (or teacher and person). A person is only temporarily a student, as presumably they graduate, eh? Is-a is a (relatively) permanent relationship from the modeling perspective, so not the best choice for ephemeral relationships. – Erik Eidt Jul 27 '17 at 17:10
  • 1
    Fully agree with Erik: the whole model is broken and should be refactored with a person having a set of roles (composition). Why ? because the identity of the person remains unchanged in principle, whereas one person can fulfill several roles (e.g. graduate student, student and teacher) which change over time. – Christophe Jul 27 '17 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.