This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Polymorphism help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Polymorphism help" Watch "Polymorphism help" New topic
Author

Polymorphism help

Brian Jeling
Greenhorn

Joined: Oct 01, 2010
Posts: 3
Hello my name is Brian. I am learning polymorphism at the moment but still unclear of the concept.
I'm having some problems figuring out what would be that advantage of using a superclass reference pointing to a subclass.
I took this snippet from the Sun Certified Java Programmer book.
The part I dont understand is why do we need to do: Animal b = new Horse() when we can easily do Horse b = new Horse() to get a Horse object?
I also looked on the JAVA API as instructed but I still have some doubts.

Thank You.



paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 19672
    ∞

Have you looked at http://www.javaranch.com/campfire/StoryPoly.jsp

??

permaculture forums
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Welcome to the Ranch

You can create a reference which is called by a particular type. Then you can make that reference point to an object of that type or any of its subclasses. If you create a reference to Animal, then all its instance methods are available. If, for example, the Horse class has a buck() method, that might not be available in Animal, so you cannot call it on an Animal reference. You can, however, call the eat() method, which is in the Animal class (suggesting that all kinds of Animal eat, but only horses buck). When you call the eat() method on an Animal reference which points to a Horse object, you use run-time binding, which finds the versipon of that method in the actual object, so you will execute the Horse version.

Beware: don't try polymorphism on static methods. It doesn't work.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 19672
    ∞

Or this: http://faq.javaranch.com/java/CodeBarnShapes
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

Sometimes, you don't know what you're going to get. Perhaps you have a collection of animals - like in a zoo. you want each animal to eat. so you write a loop (i'm using pseudo code, not actual java code)


This is much simpler and cleaner than


the REALLY cool thing is that the first way, the zookeeper can add new animals types whenever they want. You don't even have to know what it is, and you never have to touch that code. However, the second way, each time a new animal is added, you'll have to go back in and add another 'else-if' to your conditions.


Never ascribe to malice that which can be adequately explained by stupidity.
Brian Jeling
Greenhorn

Joined: Oct 01, 2010
Posts: 3
@paul wheaten - Thanks I've just read both links and it was great.

@Campbell Ritche - Thanks for the welcome and the explanation.

@fred rosenberger - Thanks for showing an example of the advantage and disadvantage of polymorphism.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
You're welcome
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Polymorphism help
 
Similar Threads
Unable to understand reason for ClassCastException
Polymorphism
which version of method is actually called ? the reference one or actual object one ?
overloaded method call rules
Polymorphism