Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
Another question is:
What are the different ways in which polymorphism can be achieved in Java. I know that having the same method name with different signatures or parameter list is one way. Is there any other way that polymorphism can be achieved in Java.
Thanks.
Matthew
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
Polymorphism can be achieved through the Dynamic Method dispath also. That is the over ridden method is resolved at the run time.Ofcourse Overloading is another concept of Polymorphism and this is also called static binding or compile time binding .
Cheers,
Gaya3.
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
Polymorphism can be achieved through the Dynamic Method dispath also. That is the over ridden method is resolved at the run time.Ofcourse Overloading is another concept of Polymorphism and this is also called static binding or compile time binding .
Cheers,
Gaya3.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can use polymorphism in code that references a base class - any derived class can be used in its place - or that references an interface - any class that implements the interface may be used in its place.
So if I have a method that takes a Shape argument and Shape is a base class, you can give that method any class that extends Shape and the method can call Shape methods on it.
If the method takes IShape argument and IShape is an interface, you can give that method any class that implements IShape. Now, if Shape implements IShape, you can pass either a derived class or something that implements IShape. You see this sometimes in the JDK with "DefaultSomething" classes.
Hope that helps!


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
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Originally posted by Mathew Kuruvilla:
Another question is:
What are the different ways in which polymorphism can be achieved in Java. I know that having the same method name with different signatures or parameter list is one way. Is there any other way that polymorphism can be achieved in Java.
Thanks.
Matthew

Strictly speaking, having the same method name with different signitures within a single inheritance tree is overloading, and has nothing to do with polymorphism.
Polymorphism is where the exact same message elicits different behavior from objects in the same inheritance tree. Simple example: the Dog and Cat classes both inherit a speak() method from the Pet class, and each overrides that method to provide its own behavior.
There are different ways this can play out:
  • The Pet class provides an implementation of the speak() method that can be overridden in the child classes, and each can implement it differently.
  • The Pet class can be abstract, and provide implementations only for common behaviors, which probably would not include the speak() method. This forces the child classes to implement their own speak() behavior.
  • Pet can be an interface that defines the signature for a speak() method, again forcing implementors to provide the behavior.

  • In these scenarios, the program can cycle through an array of Pet objects, calling the speak() method on each one, and get the appropriate behavior.
    Which one you choose depends on the problem space you are working in. In the case of my dogs and cats example, I would probably choose to have a Pet interface, and possibly an abstract Animal class. It would depend on how complex the behavior is, and how many common behaviors can be abstracted out of the concrete classes. The Pet interface could be used for something no more complex than to distinguish a feral cat from a house cat, even if they both have the exact same behaviors.
    You can see how in some cases, interfaces, concrete and abstract classes map pretty well to a description of the real-world objects, classes and relationships.
    I hope this is helpful, and more importantly, that I got it right! :roll:
    [ July 09, 2003: Message edited by: Philip Shanks ]

    Philip Shanks, SCJP - Castro Valley, CA
    My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
     
    Consider Paul's rocket mass heater.
     
    subject: Polymorphism
     
    Similar Threads
    JAVA Upcasting and Downcasting of reference variables
    How use OOPs in Servlet
    Complie time polymorphism vs Runtime polymorphism
    polymorphism
    why Object Class was chosen Parent to all java classes?