wood burning stoves*
The moose likes Java in General and the fly likes How many types of polymorphisms ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How many types of polymorphisms ?" Watch "How many types of polymorphisms ?" New topic
Author

How many types of polymorphisms ?

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
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.


Javardhan:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How many types of polymorphisms ?
 
Similar Threads
Polymorphism
MasterExam OO question
about polymorphism
object
Objects