SOFTWARE ENGINEERING

SOFTWARE DESIGN

DESIGN PATTERNS

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Favour composition over inheritance. What does that mean?
A
HAS-A can be better than IS-A.
B
IS-A can be better than HAS-A.
C
Do not use abstract classes.
D
The number of classes should be identical with the number of interfaces.
Explanation: 

Detailed explanation-1: -Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it’s easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type.

Detailed explanation-2: -Inheritance offers a way to reuse code by extending a class easily and with little effort. It is a valuable tool for establishing relationships between classes. In situations where you want to use an object as a field within another class, you should use composition.

Detailed explanation-3: -To favor composition over inheritance is a design principle that gives the design higher flexibility. It is more natural to build business-domain classes out of various components than trying to find commonality between them and creating a family tree.

Detailed explanation-4: -Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects. Whereas inheritance derives one class from another, composition defines a class as the sum of its parts.

There is 1 question to complete.