Inheritance vs Delegation:
- Inheritance: Allow one class (Child class) inherits attributes and behaviors from another class (Parent class), promoting code reusability.
- Delegation: One class delegates duties to another class which reduces coupling.
- Create a subclass of SortedIntList with inheritance that counts how many elements have been added to the list.
- Create a class to extend SortedIntList with delegation to count how many elements have been added to the list.
- Discuss questions regarding the tradeoffs and limitations between inheritance and delegation with a TA (no written answer needed).
You will examine the strengths and weaknesses of inheritance and delegation by using both techniques to add a feature to a SortedIntList class. The SortedIntList class is one of a family of integer lists. It is very similar to the AbstractIntList, except it stores its elements in ascending order.
We want to instrument a SortedIntList to count how many elements have been added since it was created. (This is not the same as its current size, which is reduced when an element is removed). To provide this functionality getTotalAdded, you should count the number of attempted insertions and also provide an access method to get this count. Your solutions should use inheritance and delegation so you can reuse, but not modify, the original SortedIntList implementation. Please refer to the appendix for the UML diagram.
We have provided an empty InheritanceSortedIntList class where you should implement an inheritance-based solution.
Hints:
InheritanceSortedIntListshould extendSortedIntList.- The
SortedIntListcontains two methods that add elements,addandaddAll.- You should override both of these methods to track how many elements have been added.
- Make sure you check that
getTotalAddedworks with bothaddandaddAll.
After you have implemented the InheritanceSortedIntList class, test the instrumentation you just added using tests in InheritanceSortedIntListTest class. You can do this via the mvn test command.
We have provided an empty DelegationSortedIntList class where you should implement your delegation-based solution.
Hints:
- Ensure your class implements the
IntegerListinterface.- Create a private
SortedIntListinstance for delegation.- Delegate list operations to the
SortedIntListinstance.- Make sure you check that
getTotalAddedworks with bothaddandaddAll.- After you have implemented the
DelegationSortedIntListclass, test the instrumentation you just added using tests inDelegationSortedIntListTestclass.
Run the tests (mvn test) and make sure your instrumentation passes all the tests.
(You might want to use the printList helper we provided you and read the documentation for addAll in the AbstractIntList class.)
Evaluate your two implementations for the given problem and answer the following questions:
- Which is more dependent on the implementation details of the
SortedIntList, delegation or inheritance? - If the
addmethod inSortedIntListis significantly modified or its behavior changes, which implementation is more likely to break? - What issues does using delegation solve that might have been problematic with inheritance?
- Based on the provided implementations, when would it be more appropriate to use inheritance and when to use delegation?
Take a look at the UML diagram to see what each function should implement.
classDiagram
direction TB
class IntegerList {
<<interface>>
+add(int) boolean
+addAll(IntegerList) boolean
+get(int) int
+remove(int) boolean
+removeAll(IntegerList) boolean
+size() int
}
class AbstractIntList {
<<abstract>>
+add(int) boolean
+addAll(IntegerList) boolean
+get(int) int
+remove(int) boolean
+removeAll(IntegerList) boolean
+size() int
}
class SortedIntLinkedList {
+add(int) boolean
+addAll(IntegerList) boolean
+get(int) int
+remove(int) boolean
+removeAll(IntegerList) boolean
+size() int
}
class SortedIntList {
+add(int) boolean
+addAll(IntegerList) boolean
+get(int) int
+remove(int) boolean
+removeAll(IntegerList) boolean
+size() int
}
class InheritanceSortedIntList {
-totalAdded int
+getTotalAdded() int
}
class DelegationSortedIntList {
-totalAdded int
+getTotalAdded() int
}
AbstractIntList ..|> IntegerList
SortedIntLinkedList --|> AbstractIntList
SortedIntList --|> AbstractIntList
InheritanceSortedIntList --|> SortedIntList
DelegationSortedIntList *-- SortedIntList
classDiagram
class ClassA
class ClassB
ClassA "1" *-- "1" ClassB : composition
class BaseClass
class DerivedClass
DerivedClass --|> BaseClass : generalization
class SomeClass
class SomeInterface {
<<interface>>
}
SomeClass ..|> SomeInterface : realization