• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the point/advantage of polymorphism?

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys and Girls, I would appreciate if somebody would please explain why you would do something like this:

Assume that the Porsche class is a subclass (extends) the Car class...


This means that 'c' can only invoke the methods available in the Car class yes? Well why not just make 'c' an instance of the Car class?



Edit: title changed when I knew what I was asking
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

By this way ,the language supports the "Polymorphism"

you can read about Polymorphism here.

Hope this helps
Thanks
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vijay.

So in my above code example,
the only reason to do such a thing (what is it called?) is because I want to have all the methods of the Car class available to me AND the availability of more specialised Porsche methods?
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..


Thanks
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you are a car collector, like Jay Leno. You want to have dozens and dozens of cars. Some will be porsches, some corvettes, some yugos. It would be nice if you could put them all into a list.

Now, Java only lets you put one type of thing into a collection. You don't want to have a porsche list, a corvette list, and a yugo list - you want a list of cars - so that's what you do. then, since all of these ARE cars, you can put them all into the collection.

Then you want to start each and every car once a week (to keep it in good condition). you want to iterate through your collection and call each car's start() method. you can do this:



There are several beautiful things about this. first, you don't need to code it like this:



Look at these two code examples. Now imagine that after a few years, we decide to buy a bmw. In the first example, i don't need to touch my code, and it will handle a bmw just fine.

That is so cool, it bears repeating. the first code example can hand object types THAT DIDN'T EXIST when i wrote it. the less I have to touch code, the less likely i am to introduce a bug.

the second code example would need to be updated each and every time I want to buy a new type of car. That sucks.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks fred, for giving a good example for the use of overridden methods in a collection.

I have used this concept of polymorphism in an insurance application which manages different types of insurance products.
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Vijay.

Thank you very much also Fred.

This has cleared lots up for me. I didn't want to advance past chapter 6 until I had cleared that up in my head.

Would you be able to suggest a new title for this thread so that others can find it?

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glen Iris wrote:Would you be able to suggest a new title for this thread so that others can find it?



"What's the point of polymorphism?"
reply
    Bookmark Topic Watch Topic
  • New Topic