aspose file tools*
The moose likes Java in General and the fly likes Regarding inheritance and composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding inheritance and composition" Watch "Regarding inheritance and composition" New topic
Author

Regarding inheritance and composition

santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 144
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.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
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.



SCJP 6, SCWCD 5, SCBCD 5
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Without composition:


With composition:


Without inheritance:


With inheritance:


SCJP 6 || SCWCD 5
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
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.

Danish Sualeh
Greenhorn

Joined: Oct 06, 2005
Posts: 11
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding inheritance and composition