• 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

Regarding inheritance and composition

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have one doubt that is, what is inheritance and what is composition and when should use inheritance and composition, can you tell me how to make composition please help me.


Thanking You.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well , I am not going to explain inheritance and composition , thousand of book have explain it with great example , one of the book is "Thinking In Java".

The thing to take note is , when considering reuse, think of composition over inheritance.


 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without composition:


With composition:


Without inheritance:


With inheritance:
 
Ranch Hand
Posts: 116
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Angstadt wrote:Without composition:


With composition:



No, not quite. Composition involves delegating to another type; by delegating to a number of different objects you achieve something similar to multiple inheritance. A simple case:
1. define interface for supertype
2. define interface for subtype, extending interface of supertype
3. implement supertype interface with a class
4. implement subtype interface with a class, without extending superclass; instantiate superclass and deleget superinterface methods to the instance.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could refer to The Strategy Pattern to help appreciate the advantage of composition over inheritance.

It will sure give you a better understanding of when to choose composition or inheritance
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic