I have doubts on approaching doing this.

I have a Train class and a Wagon class. There are three types of wagon: FirstClass, TouristClass and DiningCar. Each of these types has the same properties and methods, the only difference between them is their multiplicity with the Train class.

Each train class has one and only one DiningCar wagon, at least one FirstClass wagon and at least one TouristClass wagon.

What is the best way to do this. Should the classes FirstClass, TouristClass and DiningCar inherit from the Wagon class, make them empty and then relate them to the train class? Or there is other approach?

  • 3
    Please, read this before anything else -> softwareengineering.meta.stackexchange.com/questions/6166/…
    – T. Sar
    Commented Nov 13, 2017 at 17:16
  • 1
    What does the word "best" mean in this context? Easiest to code? Easiest to extend? Easiest to read? Commented Nov 13, 2017 at 17:17
  • 2
    If they really all behave the same, then why make them separate classes at all? You say you have these classes... Well, why? Presumably, you want to write code that uses them somehow. What will that code do? Using the language's type system to model relationships in your problem domain is not always the best way to proceed. Commented Nov 13, 2017 at 17:54
  • A minimalist solution is one car class that takes a string that says what kind of car it is. You did say the only difference was how many. Commented Nov 13, 2017 at 18:00
  • Isn't it sad when so many (most?) UML questions here come from university (assignment) context as opposed to real-world use? Commented Nov 17, 2017 at 17:09

1 Answer 1


I won't provide the diagram because this is obviously some kind of homework. But here some hints:

The basics

Each train class has one and only one DiningCar wagon, at least one FirstClass wagon and at least one TouristClass wagon.

First, intuitively, DiningCar, FirstClass and TouristClass offer different services and behaviors. For example, you wouldn't expect FirstClass to offer a function getNumberOfFreeRestaurantTables(), while you would expect DiningCar to do. So specializing the Wagon seems a good idea.

Each train class has one and only one ... at least one ... and at least one ....

You have to translate this into associations between Train and your specialized Wagon classes. For each association you can specify the multiplicity.

The more elaborate

As there is a whole-part association between Train and its Wagons, you may consider using an aggregation instead of a normal association.

As a wagon could be removed from one train and attached to another train, we could assume that aggregation is the correct alternative, and that composition would not suit the needs.

Are there other approaches ?

Sure ! You could not specialize Wagon, and let Wagon have some property that tells if which kind it is.

The aggregation association between Train and Wagon should then have a multiplicity telling that there are at least 3 Wagons (one of each).

But how would you then express that there is exactly one DiningCar at at least on of each of the other kind ? Well, you could consider writing a constraint for the association. The OCL expression to express this constraint formally is however much less self-explanatory than our initial approach with specialization...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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