aspose file tools*
The moose likes Java in General and the fly likes Polymorphism in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Polymorphism in java" Watch "Polymorphism in java" New topic
Author

Polymorphism in java

Manoj Chandran
Greenhorn

Joined: Jan 26, 2001
Posts: 25
hello,
I want to know about how polymorphism is achieved in Java.Does method overloading enables polymorphism? Is the conventional polymorphism different from run-time poly...?
Also plz differentiate between dynamic binding,run-time binding
and dynamic method dispatch...Plz reply immediately..
Thanks in advance..
Manoj Chandran.

Thanks & Regards,

Manoj Chandran
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Well, if you're willing to read something:
Inheritance, polymorphism, static/dynamic binding, etc.
a lot of Q&As, examples, explanations, code, etc.
[This message has been edited by Roseanne Zhang (edited January 26, 2001).]
Manoj Chandran
Greenhorn

Joined: Jan 26, 2001
Posts: 25
Thanks Roseanna,I'll refer the site you suggested and try to get my doubts cleared.
Manoj
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Roseanne's notes are really good reading. The not so sort version is:
polymorphism = "many shape changes"
This is achieved in Java using subclassing. A Base class can have many subclasses, each being a different variety of the Base class. So if I have a whole bunch of instances of the subclasses, I know that I can handle all of them the way I would handle a Base - CUZ they ARE Bases, just particular kinds of Bases.
There is the famous Animal Class, with subClasses Cat, Dog, Horse, etc - but they are all animals, so they all have animal attributes like species, color, sex, etc. and they all have animal behaviors like eat(), move(), play() etc.
Dynamic binding happens at run-time, static binding happens at compile time.
This is especially important to understand when putting a subclass into a variable defined as the Base class. Since variables have static binding and methods use dynamic binding they will be handled differently.
Base b = new SubClass();
When b is created the variables of Base are constructed and initialized the way a Base wants them. The system does not wait to see what will actually be in the variable at runtime. However when the SubClass is shoved into base, the methods (which are resolved at run-time) of the SubClass override the method behaviors of the Base.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism in java