Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

polymorphism

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
example for polymorphism.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaranch.com/campfire/StoryPoly.jsp
 
Get out of my mind! Look! A tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic