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-private superclass method that it doesn’t override. With composition, by contrast, the front-end class must explicitly invoke a corresponding method in the back-end class from its own implementation of the method. This explicit call is sometimes called “forwarding” or “delegating” the method invocation to the back-end object.Note:Composition means the same as:
* contains
* is part ofNote2:As you progress in an object-oriented design, you will likely encounter objects in the problem domain that contain other objects. In this situation you will be drawn to modeling a similar arrangement in the design of your solution. In an object-oriented design of a Java program, the way in which you model objects that contain other objects is with composition, the act of composing a class out of references to other objects. With composition, references to the constituent objects become fields of the containing object. To use composition in Java, you use instance variables of one object to hold references to other objects.
B
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.