File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Inheritance vs Aggregation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance vs Aggregation" Watch "Inheritance vs Aggregation" New topic
Author

Inheritance vs Aggregation

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Folks,

Polymorphism can be acheived by two means:
1.Inheritance is-a relationship
2.Aggregation has-a relationship

Inheritance is achieved by extending the existing class using the extends clause and Aggregation is by declaring a object of a class as a member in another class.

I need some clarifications on when and where to use inheritance and aggregation.

Please provide me with some examples and explanations.

Thanks & Regards,
Prasath T


Thanks,
Prasath

SCJP1.4, SCWCD
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Prasath Thirumoorthy:
...Polymorphism can be acheived by two means:
1.Inheritance is-a relationship
2.Aggregation has-a relationship
...

Uh...
Originally posted by Prasath Thirumoorthy:
...I need some clarifications on when and where to use inheritance and aggregation...

In general, favor composition (has-a) over inheritance (is-a). If you have a need for polymorphism, then use inheritance. (But you're going to need to clarify exactly what polymorphism is before this makes sense.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is an older article (from 1998), but the concepts are fundamental: Artima Developer - Composition versus Inheritance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance vs Aggregation