• Post Reply Bookmark Topic Watch Topic
  • New Topic

Aggregation vs. Inheritance  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
are there any major advantages to using one of the above mentioned over the other(i.e. Aggregation over Inheritance or vice versa) ?

if yes, can anyone highlight these for me?
thanx.

-W.O.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is like comparing apples and oranges. Aggregation is usually described in the text books as "has a". Inheritance is usually described as "is a".

An example of this is Class Car "is a" Vehicle. Thus Car would inherit from Vehicle. However Class Car "has a" Engine. Thus Engine is an aggregate of Car. You can never say an Engine "is a" Car, or vice versa.

Hope this helps.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jody Brown:
It is like comparing apples and oranges. Aggregation is usually described in the text books as "has a". Inheritance is usually described as "is a".

An example of this is Class Car "is a" Vehicle. Thus Car would inherit from Vehicle. However Class Car "has a" Engine. Thus Engine is an aggregate of Car. You can never say an Engine "is a" Car, or vice versa.

Hope this helps.


thanks jody,....
i was aware of these definitions and analogies...

however there are examples where both can apply, right?

take a class for example describing two points in a 2 dimensional coordinate system....(lets call this class: "Point")...

if we create a class "Circle" for example which has two coordinates x and y as instance variables we wouldnt be wrong if we created a "Point" object in "Circle" and solved our problem with the help of aggregation....

likewise we could extend our "Point" class by "Circle" and create a circle with our "Point" constructor (overwriting the parent class and instantiating further variables like radius, etc....)

so,technically and grammatically(at least in english) speaking a circle has a point.....
but IMHO - in a more general sense it can also "BE" a point("is a": with an additional radius -or as the case may be points)....

i hope i'm not sounding disoriented, 'cause it's a bit difficult explaining what I'm thinking

cheers

-W.O.
[ July 07, 2006: Message edited by: Wolfgang Obi ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wolfgang --

You're right that often there's a choice to make that's not so clear-cut. A guideline that can help is that if you want to extend methods and get polymorphic behavior, then choose inheritance; if you just want a class to share code from some other class, then use aggregation (and delegation.) The main reason is that inheritance is more brittle, and harder to undo as your code evolves: if class A extends class B, then that is part of its public interface, and other classes may come to depend on that. If later you decide that A shouldn't extend B, then many classes may have to change. If, instead, A had merely contained a B, then removing it won't affect any clients of A at all.

So use inheritance when it's an essential part of your design, and use aggregation otherwise. One conclusion that many people come to after years of experience is that extending a concrete (non-abstract) class is always bad; when you have the urge to do this, consider defining an interface and making both classes implement it instead, perhaps by letting them both extend a common third abstract class.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ernest,

thanx a million....
such a beautiful straight to the point answer!....

I really appreciate it.

-W.O.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!