Which four are true? (Choose four.)
-
Has-a relationships should never be encapsulated.
-
Has-a relationships should be implemented using inheritance.
-
Has-a relationships can be implemented using instance variables.
-
Is-a relationships can be implemented using the extends keyword.
-
Is-a relationships can be implemented using the implements keyword.
-
An array or a collection can be used to implement a one-to-many has-a relationship.
Has-a relationships (composition/aggregation) represent 'has-a' connections between classes and should use instance variables to hold references - option C is correct. Arrays/collections implement one-to-many has-a relationships - option F is correct. Has-a should NOT use inheritance (that's for is-a) - options A and B are wrong. Is-a relationships represent inheritance and use 'extends' for classes - option D is correct, or 'implements' for interfaces - option E is correct. So C, D, E, F are true.
Has-a relationships (composition/aggregation) are naturally implemented with instance variables, e.g. a Car has an Engine field, and collections/arrays are a common way to model one-to-many has-a relationships (a Garage has many Cars). Is-a relationships are implemented via extends (class inheritance) or implements (interface implementation), not via has-a mechanisms — which is why "has-a via inheritance" and "has-a should never be encapsulated" are false.