0
votes

I am trying to understand the relationship between classes in Object oriented world, and came across various terms like:

Association , Aggregation, Composition, Dependency, Generalization, Realization, Using (and may be there are more to the list, which I would encounter soon).

I came across the following UML diagram:

enter image description here

Here, we have two different Classes (and so objects), Car and Road, and the connector symbol connecting them (and I believe it is directed association symbol, as per MS Visio).

So this means that Car and Road classes are having some relationship (association). I have some doubts on this to understand this relation:

1) How would this relationship be translated to Java classes? I am having difficulty in understanding how Car and Road would have "some code" connecting them?

2) what does * and 0..1 mean in this diagram? Usually I have seen these in an Entity-Relationship diagrams (in DB).

Any pointer to understand this would be of great help.

4
Hmm, smells like homework. Have you actually looked at any sites that explain UML?markspace
No, not a homework. I read on-line articles, but still have doubts. The more I search on net, the more I get confused. No homework and not that I didn't do my work before asking here. This might be a naive question, however for me, at the level I am in, it is causing me lot of confusion. (lot of new symbols, vocabulary)CuriousMind

4 Answers

3
votes

A Car object would have a reference to a Road object (in other words, an instance variable of type Road). A Road object would also have a list (or List) of Car objects. The first sentence represents the 0..1 relationship between the classes; note that the reference could be null (car is on 0 roads) or not (car is on one road). The list in the Road object represents the * relationship -- 0 or more cars are on the road.

1
votes

1) is too broad to answer. UML and Java are both formal languages with well-defined structures, but there is no standardized way of expressing any particular UML concept in Java or vice versa. Thus, any answer would be opinion-based.

Furthermore, an association is a loosely-defined relationship. UML has many others more strictly defined (you've listed a few), and those are easier to translate to source code.

Because there are more strictly defined relationships, the correct reading of an association relationship is along the lines "these two things are related somehow, but not so tightly that the one contains the other, or that the one uses the other, or is dependent on the other." Those concepts all have their own connectors, and the modeller has made a conscious decision not to use them.

2) * means "any number" and 0..1 means "zero or one", which is usually read as "an optional". So the drivesOn relationship associates any number of Cars with an optional Road.

Presumably this should be taken to mean that a car may drive on a road, but never on more than one, and a road may have any number of cars driving on it.

In terms of understanding UML, this is a very poor example so don't try to read too much from it.

1
votes

Your questions do have simple answers:

  1. Your uni-directional many-to-one association drivesOn is expressed in (or translated to) Java in the form of a single-valued reference property in the following way:

    class Car {
      int passengers;
      Road drivesOn;
    }
    
  2. The symbols * and 0..1 represent multiplicities: * means many (or unbounded) and 0..1 means at most one, so your model makes two multiplicity statements: (1) a Car movesOn at most one Road, and (2) a Road has many Cars moving on it.

A pointer for reading more about the meaning of associations and multiplicities and how they are expressed in Java is my book chapter Reference Properties and Unidirectional Associations.

0
votes

It is like many cars can be associated to no road or at max one road. In other words many cars can be driven on 1 road or not at all driven on any road