aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "polymorphism" Watch "polymorphism" New topic
Author

polymorphism

madhu chakkaravarthy
Greenhorn

Joined: Sep 14, 2007
Posts: 8
example for polymorphism.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I answer this with the assumption that you know Inheritance..

Say you have a parent class Vehicle and child classes Car, Bike and Bus.

Now you are writing the method to fill gas in your respective vehicles.

Without Polymorphism, the code will be like this
//for car
void fillGas_Car(Car myCar){
//code to fill gas
}

//for bike
void fillGas_Bike(Bike myBike){
//code to fill gas
}
and similarly for Bus also........

Polymorphism makes this simple.......

//write one generic method for vehicle
void fillGas_Vehicle(Vehicle myVehicle){
//code to fill gas
}

Poly-many morph-forms..
Here myVehicle can take the form of a car or a bike or a bus or any vehicle extending from it..

I hope that makes it clear!!!


Cheers!
RSR
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://www.javaranch.com/campfire/StoryPoly.jsp


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
jQuery in Action, 2nd edition
 
subject: polymorphism