what is polymorphism?

Answer Posted / ganesh

Overloaded methods are methods with the same name signature
but either a different number of parameters or different
types in the parameter list. For example 'spinning' a
number may mean increase it, 'spinning' an image may mean
rotate it by 90 degrees. By defining a method for handling
each type of parameter you achieve the effect that you want.

Overridden methods are methods that are redefined within an
inherited or subclass. They have the same signature and the
subclass definition is used.

Polymorphism is the capability of an action or method to do
different things based on the object that it is acting
upon. This is the third basic principle of object oriented
programming. Overloading and overriding are two types of
polymorphism . Now we will look at the third type: dynamic
method binding.

Assume that three subclasses (Cow, Dog and Snake) have been
created based on the Animal abstract class, each having
their own speak() method.

Example:
--------
public class AnimalReference
{
public static void main(String args[])
Animal ref // set up var for an Animal
Cow aCow = new Cow("Bossy"); // makes specific objects
Dog aDog = new Dog("Rover");
Snake aSnake = new Snake("Earnie");

// now reference each as an Animal
ref = aCow;
ref.speak();
ref = aDog;
ref.speak();
ref = aSnake;
ref.speak();
}
Notice that although each method reference was to an Animal
(but no animal objects exist), the program is able to
resolve the correct method related to the subclass object
at runtime. This is known as dynamic (or late) method
binding.

Is This Answer Correct ?    16 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is intern method in java?

794


what is the final keyword denotes in java?

780


What do you mean by local variable and instance variable?

701


when you will synchronize a piece of your code? : Java thread

746


What are the restrictions imposed on method overriding?

770


What is the difference between JDK and JVM?

854


What is void class in java?

715


What is thread pool in java with example?

731


Variables used in a switch statement can be used with which datatypes?

718


How do I remove a character from a string in java?

702


What are static blocks in java ?

821


What is a "pure virtual" member function?

838


Why there are some null interface in JAVA? What does it mean? Give some null interface in JAVA?

853


Is there any case when finally will not be executed?

708


Is singleton set an interval?

708