Polymorphism is a Java concept that allows us to perform a single action in multiple ways. Polymorphism is made up of two Greek words: polymorphism and morphism. The words “poly” and “morphs” both mean “many.” Polymorphism denotes the presence of multiple forms.
Types of polymorphism:
Polymorphism in Java is achieved through method overloading and overriding.
A call to an overridden method is resolved at runtime rather than compile-time in runtime polymorphism or Dynamic Method Dispatch.
In this process, a super class’s reference variable is used to call an overridden method. The object referred to by the reference variable is used to determine which method should be called.
Upcasting occurs when a reference variable in the Parent class refers to an object in the Child class. For instance:
class A{}
class B extends A{}
A a=new B();//upcasting
We can use the reference variable of class type or an interface type for upcasting. For Example:
interface I{}
class A{}
class B extends A implements I{}
Here, the relationship of B class would be:
B IS-A A B IS-A I B IS-A Object
Since Object is the root class of all classes in Java, so we can write B IS-A Object.
class Car{
void run(){System.out.println("running");}
}
class Honda extends Car{
void run()
{
System.out.println("running safely with 60km");
}
public static void main(String args[]){
Car b = new Honda();//upcasting
b.run();
}
}
running safely with 60km.
class Shape
{
void area()
{
System.out.println("area of a shape:");
}
}
class Rectangle extends Shape
{
void area()
{
System.out.println("area of a rectangle:l*w");
}
}
class Circle extends Shape
{
void area()
{
System.out.println("area of a circle: pi*r*r");
}
}
class Triangle extends Shape
{
void area()
{
System.out.println("area of a triangle:(b*h)/2");
}
}
class TestPolymorphism2
{
public static void main(String args[])
{
Shape s;
s=new Rectangle();
s.area();
s=new Circle();
s.area();
s=new Triangle();
s.area();
}
}
Sr. No. | Key | Static Binding | Dynamic Binding |
---|---|---|---|
1 | Basic | It is resolved at compile time | It is resolved at run time |
2 | Resolve mechanism | static binding use type of the class and fields | Dynamic binding uses object to resolve binding |
3 | Example | Overloading is an example of static binding | Method overriding is the example of Dynamic binding |
4. | Type of Methods | private, final and static methods and variables uses static binding | Virtual methods use dynamic binding |
Note: also read about the Method Overriding in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…