Free Online Flashcard Deck

7 Composition and Delegation Free Online FlashCards

Study 7 Composition and Delegation with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

What is composition in object-oriented design?

Back

Composition builds an object from references to other objects that provide part of its state or behavior. It expresses a has-a relationship.

02
Front

How does composition differ from inheritance?

Back

Inheritance expresses an is-a relationship, while composition expresses a has-a relationship. A MountainBike may be a Bicycle, whereas a Bicycle has a Wheel.

03
Front

What characterizes strong composition?

Back

In strong composition, the containing object creates and controls the lifetime of its parts, such as an Order creating its own OrderLine objects.

04
Front

What is dependency injection?

Back

Dependency injection supplies a collaborator from outside, commonly through a constructor. This makes dependencies explicit, supports testing, and allows implementations to be replaced.

05
Front

What is delegation?

Back

Delegation occurs when an object receives a request but passes responsibility for carrying out the operation to another object.

06
Front

What role does the main object play during delegation?

Back

A delegating object may validate input or coordinate an operation, then call a method on its collaborator and return or combine the result.

07
Front

How does composition support encapsulation?

Back

Encapsulation means a containing object uses a component's methods or interface instead of directly modifying its private fields.

08
Front

Why compose with an interface rather than a concrete class?

Back

Depending on an interface lets the same class use different implementations, such as email, SMS, or a test notification sender.

09
Front

What condition supports using inheritance?

Back

Inheritance requires a genuine subtype whose instances can safely be used wherever the superclass is expected. This is the basis of substitutability.

10
Front

When is composition usually preferable to inheritance?

Back

Prefer composition when a relationship is has-a, uses-a, or depends-on; when behavior varies independently; or when a hierarchy would become rigid or deep.

11
Front

Why should Report use rather than extend Printer?

Back

A Report should use a Printer rather than extend Printer because a report is not a kind of printer. Composition models the relationship accurately.

12
Front

How does composition implement the strategy pattern?

Back

The strategy pattern places replaceable behavior in a collaborator and delegates an operation to it. UserAccount can therefore vary notification behavior without subclasses.