File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance and aggregation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance and aggregation" Watch "Inheritance and aggregation" New topic
Author

Inheritance and aggregation

Roberto Diaz
Ranch Hand

Joined: Mar 08, 2002
Posts: 40
Need some help in understanding inheritance
and aggregation. I have read lots of technical
jargon and it is all getting convoluted.
Some simple explanations will be of great help
to me.
TIA.
Zafar Ali
Ranch Hand

Joined: May 11, 2002
Posts: 64
hi Roberto Diaz , I am trying to explain .
If you know something about a category of things, you automatically know some things you can transfer to other categories.
If you know something is an animal, u take for granted that it eats, sleeps,has a way of being born, has a way of getting from one place to another... But imagine that animals , amphibians and reptiles are all animals. also cous dogs, cats are groped in category mamals. Object Orientation refers to this an "Inheritance".In Uml inheritance represented with a line that connects the parent to a child class, and on the parent's side you put an open triangel.(sorry i can't show here).
Aggregation=> Sometimes a class consists of a number of component classes. This is a special type of relationship called aggregation.


Zafar <br />***Your behaviour should affect.***
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- I'll take a shot at it
Inheritance means an "IS-A" relationship, while aggregation means "HAS-A".
To know if you've got your inheritance structure working, always ask if it makes to say,

"A subclass IS-A superclass" (but substitute the appropriate class names, of course)
For example, if Dog extends Animal, ask,
"A Dog IS-A Animal". If that does not make sense, you probably don't have a valid inheritance structure.
Here's an example that goes in the wrong direction:
Car extends Mini
Does it make sense to say,
"A Car IS-A Mini" (well, to Jessica it does... shouldn't ALL cars be Minis?
But since in the real world, NOT every Car IS-A Mini, that inheritance structure is upside down.
That example demonstrates inheritance in the wrong direction.
But what about inheritance that SHOULD have been aggregation? For example, imagine that you have an oven class that has lots of oven-related cooking code.
Now you come along and you decide to make a Kitchen class. You don't want to rewrite all that Oven code in your Kitchen class, so you EXTEND Oven and let Kitchen inherit all that Oven functionality.
Does it make sense to say, "Kitchen IS-A Oven"
No!

But does it make sense to say, "Kitchen HAS-A Oven"
Yes!
So use aggregation and let the Kitchen HAVE a Oven instance variable and everybody is happy

In general, if type X IS-A type Y, then it usually means that type X extends Y, or that type X implements Y.
But if type X HAS-A type Y, then it probably means type X has an instance variable of type Y.
Keep in mind that most (not all) good OO designs have an inheritance structure that is a kilometer wide and a centimeter deep, with aggregation a more appropriate option for most classes.
Just look in the Java API and you'll find that most clasess are only one to two levels deep. (Except AWT and Swing, of course...)
Also, consider that interfaces often make more sense than superclasses, since using IS-A with interfaces lets you be more than just a single type of thing. I can be a Human, I can be "One who can be flattened" (implements Serializable), "One who can be Remote" (RMI-able), and "One who rides horses" (implements Cowgirl), and "One who eats ice cream" (implements BenAndJerrysConsumer)
How sad for me if I had only inheritance, and thus was stuck being nothing more than a human
But thankfully, I can be all those things. However, I still cannot implement MiniDriver, but my day will come
cheers,
Kathy
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Inheritance:
You specialize a class with a subclass. An instance of the subclass "is an" instance of the class because it can receive at least the same messages(method invokations) that the superclass can. An instance of the subclass could be used wherever an instance of the superclass is acceptable --because it can receive the same messages--
When is it convenient to subclass? When you need a new class (subclass) that is very similar but not identical to an existing one (superclass). The subclass specializes the superclass: the subclass instance can override some of the method declared in superclass in a specialized way (providing implementation according to its type), it can add some state (fields) to that found in its superclass; or both.
Agregation:
It is a kind of composition. The composite class holds fields pointing to another objects. You need for your class (the composite) the abilites (methods) that another class holds. But they are not related. The referenced-to object "is not" an instance of the composite object. Thus do not use inheritance. Especially do not use inheritance to model different "states" of an object:
No -- PersonInLove extends Person
neither -- WorriedPerson extends Person
Use composition for that:
person.mood
where mood is an instance of the class Mood. mood is "part" of the "composite" object person.
Both composition and inheritance have the same goal: reuse of code. The subclass reuse the code previously written in its superclass. The composite class reuse the code written in its part clas.
Gurus recommend composition over inheritance when both are possible. The subclass is highly coupled to its superclass. The composition is a more flexible approach. Obviously things clearly hierarquically related must be modelled with inheritance.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance and aggregation
 
Similar Threads
is-a and has-a relationship
Relationship b/w Implmenting Class & Interface?
Inheritance vs Aggregation
Class Diagram -Association & Inheritance
n-ary association