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 are the rules for variable declaration?
What is the synonym of string?
What is javac in java?
How do you escape a string?
Which collections are thread safe in java?
Is 0 an irrational number?
What is indexof?
What one should take care of, while serializing the object?
Write a program to print fibonacci series
What are data types in oop?
What is the difference between abstract class and interface1? What is an interface?
Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?
What is complexity in java?
Is integer a class?
What is lifetime variable?