Question 1
A financial services company is developing a framework where different asset classes must be represented. A base Asset class is defined. Stock and Bond classes inherit from Asset. A Portfolio class is then created, which holds a collection of Asset objects. Which two primary OOP principles are demonstrated by the Portfolio class's relationship with the Asset, Stock, and Bond classes?
Answer and explanation
Correct answer: B
The Portfolio class holding a collection of Asset objects is a classic example of Composition (a 'has-a' relationship). Since the collection can contain objects of subclasses like Stock and Bond, and the Portfolio can operate on them through the common Asset interface, this demonstrates Polymorphism. Inheritance is shown by Stock and Bond extending Asset, but the question specifically asks about the Portfolio's relationship.