aspose file tools*
The moose likes Beginning Java and the fly likes inheritance/composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance/composition" Watch "inheritance/composition" New topic
Author

inheritance/composition

Nancy Bradley
Ranch Hand

Joined: Feb 25, 2004
Posts: 32
Hello, I am a little confuse, Is inheritance the same as "is a" relationship and composition the same as "has a" relationship?
Also I read about polimorphism, but I do not hava it clear. Can anyone give me an example?
Thanks for your time
Nancy
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Polymorphism is easy: Two methods have the same name, but take different parameters. For example:

Interestingly, System.out.println is itself polymorphic, as it can take anything as a parameter and treat it appropriatly.

Inheritance is an "is a" relationship, as you said, and composition can be thought of as a "has a" relationship, although all composition means really is that one class has as a member variable another class.


I've heard it takes forever to grow a woman from the ground
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
No its not correct Joseph George. Its method overloading not polymorphism. In polymorphism there involve following things.
1 - Inheritance i.e. classes should be inherited from some top level classes.
2 - Method Overriding i.e. lower level classes should implement their own definition of methods(1 or more) declared in top level class.
In polymorphism we have the variable of parent class and can store the objects of any child class of that parent. So when we call the any method using that variable the method of repective object is called i.e. overridden method.
Nancy Bradley,
You can find a lot of examples and articals on internet. Search on google or goto http://java.sun.com.
[ April 27, 2004: Message edited by: Ali Gohar ]
Nancy Bradley
Ranch Hand

Joined: Feb 25, 2004
Posts: 32
Thank you
Nancy
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
For a good introduction to the subject of Polymorphism, I recommend reading the "How my Dog learned Polymorphism" story in the JavaRanch Campfire Stories.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
of course you are right, Ali... appologies
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"is a" usually works for inheritance. As you get more advanced you may find some situations where it's not literally so. It may be interesting to think about a couple verbs - generalize and specialize - that describe how we wind up with inheritance.
If we see several classes doing the same thing, we can "generalize" and put those common things in a common super class. Frinstance, I made database helpers that could connect to Oracle, DB2, ODBC, etc. called OracleThing, DB2Thing and so on. They all did some common stuff, so I moved that stuff into AbstractThing. Is OracleThing an AbstractThing? Hard to say. Abstract thing is not complete enough to ever exist.
If we find a class getting a lot of if tests to do different behaviors, we might "specialize" the different behaviors into several extended classes. Again, we may leave the base class incomplete and a little vague about the "is a" thing.
Hope that made sense. If not, use the "is a" rule until you find a reason not to.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance/composition