• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many types of polymorphisms ?

 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

One person asked me in how many ways we are implementing polymorphism in Java?

I know only through overriding we are achieving polymorphism.
I think overloading is not kind of polymorphism (right or wrong )

Psl let me know if you are aware of any other ways of achieving polymorphism.

Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you say, overriding.
Overloading isn't polymorphism becase the overloaded methods are in the same class as each other.
You will doubtless have heard the Design Patterns chaps' mantra
Favour composition over inheritance.

You can use something like this:-
Engine<-----------------Car
^
DieselEngine OttoEngine WankelEngine

As you know, Diesel Otto and Wankel were three German chaps who invented the engines which bear their name, Otto being the 4-stroke petrol (gas) engine and Wankel the rotary petrol (gas) engine. Now you can have different behaviours for each type of engine.

Now, you can have such silly code for any different kind of engine, in which case you are using polymorphism at the Car level, but it is actually polymorphism at the Engine level.
You can have the same for class MotorBicycle; motor cycles use Otto engines, or two-stroke engines. I am not sure whether you actually call it polymorphism in these circumstance.

CR

I tried editing the original post and bits went missing, so I have tried to reconstruct what I meant to say originally.
[ June 02, 2006: Message edited by: Campbell Ritchie ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some people -- and I am definitely not one of them -- like to call method overloading "static polymorphism" or "compile-time polymorphism", and overriding "dynamic polymorphism" or "runtime polymorphism". So a person asking you "how many kinds are there?" either wants to hear two or one.

Personally I dislike the idea, and prefer to think of polymorphism as referring only to runtime polymorphism.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there other words for untyped languages?

I used to enjoy "polymorphism by coincidence" in VB4. There was no inheritance but you could try to call a method on any object. If it worked, hey, polymorphism! If not you handle the error and go on your way. So throw that in your answer and see what they say.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Are there other words for untyped languages?

I used to enjoy "polymorphism by coincidence" in VB4.


The Rubyites call this "duck typing" -- if it walks like a duck, and quacks like a duck, then it's a duck.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic