• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Composition versus Inheritance

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain following statement :
"Favour object composition over inheritance"
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "Design Patterns" book says "inheritance violates encapsulation". The problem is that when you inherit from another class, you tightly couple to the implementation of that class. When you use composition, you are tied only to the interface of a class. One of the key principals of OO is that we should always code to an interface and never to an implementation. This does not mean that you should never use inheritance but that you should only use it when it makes sense.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very valid point.
thats why we see so many design patterns using composition so much. for example important design patterns like fly weight, decorator etc use it so much.
 
Author
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mini mehta:
Can anybody explain following statement :
"Favour object composition over inheritance"


On top of what has been mentioned, inheritance is a static notion, while composition is a dynamic one; more specifically, having an instance of a subclass, this instance cannot be easily transfered to an instance of another subclass at run-time (ie. change of types at run time is hard to do with this setting). With composition, you can associate another component instance (which implements same interface) at run time.
Change of types at run time can be design and implemented using the State pattern though; how? using composition where the component keeps track of the type at run time.
But inheritance definitely has its place in design of classes, where the IS-A of model subclass is stable and to exploit polymorphism. A careless use of inheritance can give rise to class explosion in class hierarchies; for example when using inheritance to model options of features in an object.

------------------
Jaime Nino
Author of:
An Introduction to Software Construction with Java
 
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent explanations about no need of inheritance.
Can anybody explain me how "Inheritance breaks encapsulation"?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the topic started by Jose Botella a few days back.

Originally posted by Guennadiy VANIN:
Excellent explanations about no need of inheritance.
Can anybody explain me how "Inheritance breaks encapsulation"?

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic