polymorphism |
Polymorphism in Java
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 one 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 type and 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 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.
In simple words, polymorphism is the ability by which, we can create functions or reference variables that
behave differently in a different programmatic context.
Polymorphism is one of the
major building blocks of object-oriented programming along with inheritance,
abstraction, and encapsulation.
Important points
1. Polymorphism is the ability to create a variable, a
function, or an object that has more than one form.
2. In java, polymorphism is divided into two parts:
method overloading and method overriding.
3. Some may argue that method overloading is not
polymorphism. Then what does the term compile-time “polymorphism” means??
4. Another term operator overloading is also there, e.g.
“+” operator can be used to add two integers as well as concat two sub-strings.
Well, this is the only available support for operator overloading in java, and
you can not have your custom-defined operator overloading in java.
What is polymorphism in programming?
Polymorphism is the capability of a method to do
different things based on the object that it is acting upon. In other words,
polymorphism allows you to define one interface and have multiple
implementations. As we have seen in the above example that we have defined the
method sound() and have the multiple implementations of it in the different-2
subclasses.
Which sound() method will be called is determined
at runtime so the example we gave above is a runtime polymorphism example.
Concept of Polymorphism |
Types
of polymorphism and method overloading & overriding are covered in the
separate tutorials. You can refer them here:
1. Method Overloading in
Java – This is an
example of compile-time (or static polymorphism)
2. Method
Overriding in Java –
This is an example of runtime time (or dynamic polymorphism)
3. Types
of Polymorphism – Runtime and compile-time – This is our next tutorial where we have
covered the types of polymorphism in detail. I would recommend you to go
through method overloading and overriding before going through this topic.
Example of Polymorphism |
Let's write down the complete code of it:
Example 1: Polymorphism in Java
Runtime Polymorphism example:
Animal.javapublic class Animal{
public void sound(){
System.out.println("Animal is making a sound");
}
}
Horse.java
class Horse extends Animal{
@Override
public void sound(){
System.out.println("Neigh");
}
public static void main(String args[]){
Animal obj = new Horse();
obj.sound();
}
}
Output:
Neigh
Cat.java
public class Cat extends Animal{
@Override
public void sound(){
System.out.println("Meow");
}
public static void main(String args[]){
Animal obj = new Cat();
obj.sound();
}
}
Output:
Meow
Example 2: Compile time Polymorphism
Method Overloading on the
other hand is a compile time polymorphism example.
class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}
Here the method runtime compile
time so this type of polymorphism is known as compile time polymorphism. Output:
demo()
is overloaded 3 times: first method has 1
int parameter, second method has 2 int parameters and third one is having
double parameter. Which method is to be called is determined by the arguments
we pass while calling methods. This happens at a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25
Now that you have understood basics of Java, check out the Java Internship by Surya Informatics , a trusted Internship company with a network of more than 250,000 satisfied Java Developers spread across the globe.
FOLLOW US ON : Youtube, Twitter, Facebook, LinkedIn