Answer Posted / josh
Inheritance can lead to a combinatorial explosion of
classes. Composition should be somewhere in the code
oftentimes. For example, an Employee object can "contain" a
PaymentType object, a PaymentSchedule object, and a
PaymentMethod object. Each of those 3 objects are their own
classes. The Employee class creates instance of those
classes and "holds" on to them. From there, a higher class
such as a PayrollSystem can look at an Employee object and
query that Employee about that Employee's information
(namely its object contents). With composition, your code is
highly decoupled, meaning that those 3 object held by
Employee can easily be held by some other class (hence code
reuse). It is important to note that PaymentSchedule,
PaymentMethod, and PaymentType should be interfaces which
have multiple concrete implementations to them (ie. a
PaymentType interface could be implemented with an Hourly,
Salary, or SalaryWithCommission class for a given Employee).
Inheritance causes high coupling and reduces code
reusability. It helps to write a UML static(class) diagram
before typing up code
| Is This Answer Correct ? | 10 Yes | 5 No |
Post New Answer View All Answers
What is mean by collections in java?
What are jee technologies?
What is this () in java?
Is string a wrapper class?
What is meant by collection in java?
What is lambda in java?
What is a null point?
What is the scope or life time of instance variables?
What is not object oriented programming?
What is javac in java?
How do you take thread dump in java?
What are the pillars of java?
What is r in java?
Can we use both this () and super () in a constructor?
Explain the difference between extends thread vs implements runnable in java?