Top 10 Polymorphism Definitions
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any java object that can pass more than on IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.
Polymorphism is the association between a generalized reference and a more specific object. For instance, consider the following code:
Animal a = new Dog(); //right
Dog d = new Animal(); //wrong
In the first example, the reference ais referencing a Dog object. So when a method call is made, such as:
a.eat();
the eat method is carried out by the Dog object that a references.
The second example is incorrect, because not all Animals can perform Dog actions, so a method call of d.bark() does not make sense, because not all Animals can bark.
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.
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("Ernie");
// 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.
Polymorphism means when an entity behaves differently depending upon the context its being used.Moreover In other words Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. Means polymorphism allows you define one interface and have multiple implementation. That being one of the basic principles of object oriented programming.
The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime not at the compile time.
The Java engine is able to dynamically, at run time, choose one of several method definitions to execute for a single method call. This capability is called
polymorphism.
Polymorphism is a way of giving a method one name that is shared up and down an object hierarchy, with each object in the hierarchy implementing the method in a way appropriate to itself. Polymorphism applies only to a specific set of methods.
Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL).
