Question about Class diagram

Hello, i was wondering if in class diagrams , we always had to put the constructor of the class in its methods or if it can be ommited. I’am asking this because in the solutions, sometimes it is there, like for example in tutorial sheet 1, task a with classes like Muesli, Order and Guest, and not there in other ones like Shop or Account.

Regards,

Nathan

Hi,

classes always have a default constructor. This means the default constructor takes all fields of your class as an input. In this case you don’t have to model the constructor in your class diagram.

In case you want to have a different constructor, you must show that in your class diagram:

Example class CarFactory has a list of cars. The default constructor would be this: CarFactory(cars: List). You don’t have to model this. However since a car factory normally is created without cars you might wanna update the constructor to CarFactory(), this must be modeled explicitly.

3 Likes