Which is a key aspect of composition?

Which is a key aspect of composition?

Which is a key aspect of composition?

A.
Using inheritance

B.
Method delegation

C.
Creating abstract classes

D.
Implementing the composite interface

Explanation:

In the composition approach, the subclass becomes the “front-end class,” and the superclass
becomes the”back-end class.” With inheritance, a subclass automatically inherits an
implemenation of any non-privatesuperclass method that it doesn’t override. With composition, by
contrast, the front-end class must explicitlyinvoke a corresponding method in the back-end class
from its own implementation of the method. This explicitcall is sometimes called “forwarding” or
“delegating” the method invocation to the back-end object.Note: Composition means the same as:
* contains
* is part of
Note 2: As you progress in an object-oriented design, you will likely encounter objects in the
problem domainthat contain other objects. In this situation you will be drawn to modeling a similar
arrangement in the design ofyour solution. In an object-oriented design of a Java program, the
way in which you model objects that containother objects is with composition, the act of
composing a class out of references to other objects. Withcomposition, references to the
constituent objects become fields of the containing object. To use compositionin Java, you use
instance variables of one object to hold references to other objects.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


gelete

gelete

B.

OCA/OCP Java SE 7 Programmer I & II Study Guide (Kathy Sierra, Bert Bates)
Object Composition Principles, 548 Chapter 10: Advanced OO and Design Patterns

public class MailerBox implements Box, Mailer { // MailerBox IS-A Box
private Box box; // MailerBox HAS-A Box

You can see the composition part, the terminology for object composition:
MailerBox both IS-A Box and HAS-A Box .
MailerBox is composed of a Box and delegates to Box for logic.

We delegate to Box to actually do the work. This is called method forwarding
or method delegation. These two terms mean the same thing.