177k views
0 votes
What object oriented feature can we use to have a single test program, with one set of method calls in which the user can decide which bag implementation (array- or link-based) needs testing?

2 Answers

1 vote

Final answer:

To test different bag implementations with a single test program, polymorphism is used by creating a common interface for different bag types and instantiating the desired implementation based on user input.

Step-by-step explanation:

In object-oriented programming, to have a single test program with one set of method calls capable of testing either an array-based or link-based bag implementation, you would use the feature known as polymorphism. This allows you to write code that can operate on objects of multiple types that share a common interface. For instance, you can define an interface Bag that specifies all the methods that any bag should have, such as addItem, removeItem, etc. Both your array-based and linked-list-based bags can implement this interface. In your test program, you declare your variable to be of type Bag. Then, based on the user's choice, you can instantiate this variable as either an instance of the array implementation or the linked list implementation. As a result, regardless of the user's choice, the interface's methods are called, and polymorphism ensures that the specific implementation's methods are executed.

User Bactisme
by
6.5k points
4 votes

Answer:

The answer is "Polymorphism".

Explanation:

Polymorphism is a feature of the object-oriented programming (OOPs) language, in which data or an object is dividing into more than one form. In other words, we can say that the capacity of various objects to obtain and react in different ways is polymorphism. It enables interaction with both objects and common interfaces that hide various details of their implementation.

User Facebook
by
7.1k points