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
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
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 ]
"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