educative.io

Educative

In the below example, only Flight class knows about Aircraft; hence it is a uni-directional association

Hi,

The below line is not clear:
“In the below example, only Flight class knows about Aircraft; hence it is a uni-directional association”.

I see that the Aircraft class has a function “getFlights(): list”. How will this function work if Aircraft class doesn’t know about flight? I couldn’t understand why this relationship is uni-directional association.

Kindly let me know. Thanks in advance.

2 Likes

@Educative_CS & @Design_Gurus

Would appreciate if you answer the above question.

Wondering the same! @Design_Gurus @Educative_CS

Uni-directional association is a little tricky. If two classes have references of each other then we say that they have the bi-directional association between them.

In the “flight- > aircraft” case, we will have a reference (a class member) of aircraft in flight but not vice versa, that is why we will say that it will be uni-directional. Furthermore, if a class-function is using another class in function parameters or as a return type (as in this case), in UML terms it is not considered an association.

Take a look at this example for more details: https://stackoverflow.com/questions/15487372/bi-directional-and-uni-directional-associations-uml

Thanks but still find it confusing. How are you able to implement getFlights() in aircraft without storing reference of flights inside aircraft? @Design_Gurus

Here ‘reference’ means as a class member variable. We will not have a flight member variable at class level. Flight will only be used inside a function getFlights(), which according to UML definition is not considered association. If Aircraft has a member variable (at class level), then we could have said that there exists an association from Aircraft -> Flight.

Thanks for the answer. but im still confused about have a “member variable at class level”. Does that mean a variable of Type class A as a attribute of class B definition?
If thats the case, for the bidirectional association between FlightInstance and Pilot (used as an example in the same paragraph). Shouldnt the FlightInstance class have an attribute of Type Pilot and vice versa?

I am unable to understand this. According to your explanation, Flight class should have a reference to Aircraft, but in the diagram, I cannot see any class variable reference for Aircraft in Flight class?

@Design_Gurus @Educative_Team please help.

1 Like