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 return in java?
What is meant by vector class, dictionary class, hash table class, and property class?
What is merge sort in java?
What modifiers may be used with an inner class that is a member of an outer class in java programming?
What are classloaders?
Does treeset allow null in java?
What is the point of java?
What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?
Can we inherit a class with private constructor?
What is super keyword explain with example?
What is the difference between length and length () in java?
Can you sort a list in java?
What is a byte array?
Why main method is called first in java?
What is the difference between notify and notifyall method?