File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Polymorphism help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 20494
    ∞

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

??

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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: 20494
    ∞

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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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: 38033
    
  22
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Polymorphism help
 
Similar Threads
Unable to understand reason for ClassCastException
overloaded method call rules
Polymorphism
Polymorphism
which version of method is actually called ? the reference one or actual object one ?