• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Composition versus Inheritance

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Composition is considered better than inheritance as it allows runtime implementation behaviour changes.

I have a doubt , how does runtime implementation behaviour changes are done using composition?

 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you read this statement and in what context was it used?

Or, did you write this statement yourself?

In general, composition is favored over "concrete" inheritance for certain situations. Note that I have specified "concrete."

However, in some cases inheritance is appropriate and presents a cleaner design. So, it depends upon the requirements.
In a case where both design strategies can be applied, then composition is considered better. I consider it "better", in these cases, because it eliminates some syntax-level dependecies and makes code more agile/flexible, i.e. the ability to handle change in the future is increased
 
Richa Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James.



James Clark wrote:Where did you read this statement and in what context was it used?

Or, did you write this statement yourself?




I read "Favour composition over Inheritance" in Gang of Four Design patterns.

I was a bit confused with the Strategy Pattern as that uses composition over Inheritance.



 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Did you read this in the famous GOF book published 15 years ago? If not, where?
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an article where eric gamma (one of the GOF) discusses this: http://www.artima.com/lejava/articles/designprinciples4.html
 
Richa Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic