Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance vs Aggregation

 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an older article (from 1998), but the concepts are fundamental: Artima Developer - Composition versus Inheritance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic