This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance/composition

 
Nancy Bradley
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
Nancy
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a good introduction to the subject of Polymorphism, I recommend reading the "How my Dog learned Polymorphism" story in the JavaRanch Campfire Stories.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course you are right, Ali... appologies
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic