60.6k views
5 votes
Which describes the relationship when a sorted list class is composed of an instance of the class linkedlist?

1) is-a
2) has-a

User StalkAlex
by
7.8k points

1 Answer

3 votes

Final answer:

A sorted list class composed of a LinkedList illustrates a 'has-a' relationship because the sorted list owns an instance of LinkedList as one of its fields.

Step-by-step explanation:

When a sorted list class is composed of an instance of the LinkedList class, this illustrates a has-a relationship. A has-a relationship, also known as composition, occurs when a class uses an instance of another class as a field or property. For instance, if we have a SortedList class, and it contains a field that is an instance of LinkedList, the SortedList effectively has a LinkedList as part of its state. A has-a relationship is established because the SortedList does not extend the LinkedList; rather, it integrates the LinkedList as part of its functionality. A sorted list class composed of an instance of the class LinkedList demonstrates the has-a relationship.

In object-oriented programming, the has-a relationship refers to a class having a member variable of another class type.

For example, the sorted list class may have a member variable called 'list' of type LinkedList, indicating that it has-a LinkedList object that it uses to implement its sorting functionality.

User Chanpory
by
8.1k points