Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphism

 
madhu chakkaravarthy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example for polymorphism.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com/campfire/StoryPoly.jsp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic