permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is aggression and how it differs from inheritence... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is aggression and how it differs from inheritence..." Watch "what is aggression and how it differs from inheritence..." New topic
Author

what is aggression and how it differs from inheritence...

vikas mehta
Greenhorn

Joined: Dec 28, 2001
Posts: 4
what is aggression and how it differs from inheritence...
i read khalid mughal...but still not clear..
then solved 3 questions in khalid mughal on aggression but all wrong..
i m not able to understand the concept..
I M VERY CLEAR, STILL, ABOUT INHERITENCE THOUGH...
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I'm sure the term you intended is aggregation. Building a class by aggregation (sometimes also referred to as composition) simply means building a class by composing it from other classes, rather than from inheritance.
Component models rely heavily on composition, where the class that puts all things together is viewed as a container. In physical terms, one intuitively useful model is a Car. A Car can be defined as composed of several objects (parts), and it's behavior derived from the interaction of those parts to form the whole. Compare that to inheritance, where perhaps we derive behavior from a Transport interface or Vehicle parent class. The end result of the two approaches might be the same, from the perspective of its user, but the development of either type would naturally be quite different.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
the concept of inheritance is understood well when we do, like
class A extends B
so basically class A inherits the methods of class B and also can call or refer to the variables/fields in B.
how do we or what is done in aggregation ? is it done via
interfaces ?
Originally posted by Michael Ernest:
I'm sure the term you intended is aggregation. Building a class by aggregation (sometimes also referred to as composition) simply means building a class by composing it from other classes, rather than from inheritance.
Component models rely heavily on composition, where the class that puts all things together is viewed as a container. In physical terms, one intuitively useful model is a Car. A Car can be defined as composed of several objects (parts), and it's behavior derived from the interaction of those parts to form the whole. Compare that to inheritance, where perhaps we derive behavior from a Transport interface or Vehicle parent class. The end result of the two approaches might be the same, from the perspective of its user, but the development of either type would naturally be quite different.

Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
Inheritance infers an "is a" relationship!!
For example: public class Dog extends Animal
In this example we would say a Dog "is a" Animal because of its relationship to Animal.
Aggregation infers a "has a" relationship!!
For example:
public class Dog extends Animal{
Paw leftPaw = new Paw();
Paw rightPaw = new Paw();
Paw backLeftPaw = new Paw();
Paw backRightPaw = new Paw();
}
In this example, we would say that a Dog has paws. Moreover, other animals, like tigers, bears, etc. also have paws.
So, aggregation refers to how objects are used to create other objects (i.e. composition).
Hope this helps.


Hope This Helps
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
Rodney:
your example is ok. but then where is the class "Paw" defined ?
Paw is also a class. right ? is class Paw a separate class or it would under some heirarchy.
i hope the question makes sense. if not then let me know.

Originally posted by Rodney Woodruff:
Inheritance infers an "is a" relationship!!
For example: public class Dog extends Animal
In this example we would say a Dog "is a" Animal because of its relationship to Animal.
Aggregation infers a "has a" relationship!!
For example:
public class Dog extends Animal{
Paw leftPaw = new Paw();
Paw rightPaw = new Paw();
Paw backLeftPaw = new Paw();
Paw backRightPaw = new Paw();
}
In this example, we would say that a Dog has paws. Moreover, other animals, like tigers, bears, etc. also have paws.
So, aggregation refers to how objects are used to create other objects (i.e. composition).
Hope this helps.

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Aggregation simply refers to the "containment" of another class within a class. Using the previous example (the dog), the class Dog contains references to a class Paw. That Paw class may extend another class or contain classes of its own - that really doesn't matter. What we're looking at is the fact that the Dog class contains a reference to a Paw object, which is the essence of an aggregation relationship. A Dog has a Paw.
For me, understanding the difference between aggregation and inheritance comes down to understanding the phrases "has a" and "is a." With inheritance, you have a "is a" relationship and, with aggregation, you have a "has a" relationship. Let me try to explain.
Inheritance
Suppose we have a class Animal, which has a few basic traits and behaviors, like this:

This basically states that all animals will have a name, a species, a number of legs, and might or might not have a backbone. Also, all animals are capable of eating and sleeping. Now, let's create a Dog class which inherits from Animal.

In this case, the class Dog extends Animal. Not only does every Dog have all of the attributes and behaviors of an Animal (it can eat, sleep, and has a number of legs), it also has attributes and behaviors specific to a Dog (it has a license number and can fetch). In this case a Dog "is a" type of Animal. This is inheritance.
Aggregation
Aggregation is a "has a" relationship. This relationship means that one class contains a reference to another class. Let's make up a new class and modify our Dog class a little but in order to create a working aggregation relationship. First, let's create a Person class.

Now, let's update our Dog class to have an owner variable, which is of type Person:

Now, you see, the Dog class contains a reference to a Person class (the dog's owner, in this case). The Dog "has a" reference to a Person. This is aggregation.
In fact, in the last example, the Dog class exhibits both an inheritance relationship and an aggregation relationship. The Dog class "is a" type of Animal (the inheritance relationship) and it also "has a" reference to a Person (the aggregation relationship). There is no limit to how many aggregation relationships a class can have (it can contain as many references to as many different classes as you'd like), but each class can only inherit from a single class.
Hopefully, you see the aggregation relationship in the second example, when we added a reference to a Person class, but, if you look back at the first time we defined the Dog class, you'll see that it actually exhibited an aggregation relationship there, as well. String is just another class is Java and, since the Dog class contained two String references (one for licenseNumber and one for ownerName), it was already exhibiting an aggregation relationship. I just defined a Person class and used that to emphasize the point.
I hope this helps you clear up the differences between inheritance and aggregation. Be sure to have a good grasp on both as they're essential when it comes to creating OO software.
Corey


SCJP Tipline, etc.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Let's take this up one more notch. Consider whether you want to write a Dog class according to its general type (inheritance) or according to the collection of elements that make it up (composition).
Conceptually, we can classify a Dog as a Canine, which is a Mammal, which is an Animal. In a programming model, each parent type confers behavior and state to its subtypes; it is then possible to abstract all concrete subtypes and move generalizable code "up" the chain. So when we want to create collections easily, and or code general behavior (eat(), sleep()) without being bothered by details ("how" dogs eat and sleep as opposed to wolves, sharks, beetles, etc.), inheritance has many advantages. There's no physical thing that "is" an Animal, but when we want to count all the Animals in a zoo, iterating an Animal list is easier than iterating the Monkey list, then the Lion list, then the Otter list, and adding it all up.
We could also describe a Dog as something that has paws, legs, a tail, head, ears, etc. And if anatomical analysis, say, suits our purpose, that might be a way to go instead.
But there's nothing in practice that says composition and aggregation must be applied in a mutually exclusive manner. Who says a Dog class can't implement an Animal interface and be composed of parts? Answer: your end goals for the development process. If you favor rapid deployment and redployment of an ever-changing product, composition rules. Inheritance models tend to favor large apps with centralized control for things like versioning (e.g., Swing, AWT, RMI, event handling).
But there are no hard and fast rules if the programmer is utilizing both strategies where they "make sense." Consider how almost any object is Serializable, and while that makes it easy for the VM to walk through object graphs (to see if serializing a complex object is actually feasible), the interface itself poses no requirements. Neither does the Remote interface in java.rmi. With a tagging interface, you have both inheritance and aggregation at work in whatever you write.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited December 28, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is aggression and how it differs from inheritence...
 
Similar Threads
Plz guide me on how to prepare for the SCJP...
Arjun
Which is Better K&B or K Mugal?
prometric center in delhi
Passed SCJP 1.4 with 96%