5

What are the advantages/disadvantages of Option 2 over 1 in this example?

Option 1 (Inheritance):

public class SalesList : List<Sales>
{
    //methods that add extra behavior List<Sales>
}

Option 2 (Composition):

public class SalesList 
{
    private List<Sales> _list;
    //methods that add extra behavior to List<Sales>
}
derdo
  • 1,036
  • 12
  • 21
  • possible duplicate of http://stackoverflow.com/questions/1598722/should-i-use-inheritance-or-composition – Lucero Apr 06 '10 at 17:51

5 Answers5

2

Inheritance allows us to form a hierarchy of classes,such that parent is a *super*set and all its child's are *sub*set,which allows generalization,Specialization and to apply substitution principle

Murthy24
  • 21
  • 1
2

The advantage of option 2 is agility. Using composition, you can choose which parts of the list interface you expose.

Imagine that later on you decide no longer to extend from another class. By now, some of the users of your library will have used methods provided by List, so you need to implement all of the list methods yourself: add/addAll/contains/retainAll. That's a whole lot of methods to implement for a simple sales list.

Basically, this comes down to "when in doubt, leave it out" which Joshua Bloch talks about in Bumper-Sticker API Design. Every facet of an API should be as small as possible, but no smaller. You can always add things later, but you can't take them away.

Frederik
  • 14,156
  • 10
  • 45
  • 53
2

The main disadvantage of Composition is that you need to wrap (duplicate) all the public methods of the private List if you need to present the same interface, in Inheritance you have all of them already available, but you can't override any of them that was made not overridable (in C# it means the method should be marked 'virtual', in Java it can't be marked as 'final').

In C# 3 and up, you can use Extension Methods, which give the appearance of inheritance without truly messing up the hierarchy tree.

Monoman
  • 721
  • 10
  • 12
  • A bigger disadvantage is that one will not be able to pass a `SalesList` to any method which is written to expect a `List` or generic `List`. Rewriting all the `List` methods may be annoying, but hardly impossible. If a method to which one does not have the code expects a `List`, using that method may be difficult or impossible. Leaking a reference to the internal list may seem to fix that problem, but creates many more of its own. Because so many methods of `List` are non-virtual, a derived class can't change its behavior much; one may be stuck no matter what one does. – supercat Jan 18 '13 at 21:02
1

Option 1
- Advantages - all advantages of inheritance & re-use
- Disadvantages - the code is now signalling to the consumers that it is dervied from List. If this needs to change at a later date, then all the related consumers will be affected.

Option 2
- Advantages - the implementation detail about the storage of the sales data is abstracted aways from the consumer. So, if the implementation needs to change (say a dictionary, for example), the consumer of the class will be immune from these changes.
- Disadvantages - the SalesList class will now need to expose additional methods to get and/or set to the inner _list object. this is additional code which need to be maintained. Also, if the inner implementation changes, you need to take care to still support the previous behaviour...

Just a few thoughts that come to mind.

HTH.

Sunny
  • 6,286
  • 2
  • 25
  • 27
0

In composition you can change the super class implementation dynamically at run time. But using the inheritance you can not change the super class implementation at run time.

In composition it depends on what type of object you are passing in to the setter and it will behave based on that. It will give more flexibility on implementation

akjoshi
  • 15,374
  • 13
  • 103
  • 121