Polymorphism in Java
Definition :- It means that One name and more then forms that is called polymorphism, let's see an example A Programmer (You), and for another persons You are friend of friends and a Boyfriend of GF, Son of Your Father/Mother etc. So Your role is different - different platform. Read more.....
Post-Intro:- In this post we will learn what is Polymorphism why is important where is used and how to work in java with an example so let's go start now.
Table Of Content:
Java Polymorphism in Hindi
Java Polymorphism in English
Types of Polymorphism in Java
- Operator Overloading
- Function Overloading
- Run time Polymorphism
- Virtual Overloading
Java Polymorphism in Hindi
Java Polymorphism in English
- It means one name many forms so we can say that in this type of programming same function is used to perform different kind of operation.
- It is an important part of object oriented programming language.
- The term of "Polymorphism" is the combination of "Poly" +"morphs" which means many forms. Actually it is a Greek Word.
- In OOP, we use 3 main concept in Inheritance, Encapsulation and Polymorphism.
- A man behaves like a leader in our office and in home a father and son with a customer in market, so here a single person is behaving differently according to the situations.
- Let's See an Example of Polymorphism.
Example :
- Let's take a simple example in which as you can see that class name is poly and there are four function with same name a() with different parameter so execution of function is based on the value passing at the time of calling.
class Poly{
void a()
{
System.out.println("No parameterize");
}
void a(int i)
{
System.out.println("Integer parameterize");
}
void a(char ch)
{
System.out.println("Character parameterize");
}
void a(float f)
{
System.out.println("Float parameterize");
}
void a(double d)
{
System.out.println("Double parameterize");
}
void a(String s)
{
System.out.println("String parameterize");
}
}
class DemoPoly{
public static void main(String[] args)
{
Poly obj=new Poly();
obj.a(12);
obj.a('e');
obj.a("DemoPoly");
}
}
Example Explanation :
- Here function is called three times first time with integer value and the second time with character value and third time with string value so the output is:
Output :
Integer parameterize
Character parameterize
String parameterize
Types of Polymorphism
- There are two types of polymorphism compile time and run-time polymorphism. so Java support both types of polymorphism.
1.Compile time Polymorphism with Example
A. Function Overloading
B. Operator Overloading
A. Function Overriding
1. Compile time Polymorphism
- It is already known where is mistake before run (spelling, comma) which means that function will run compile time so it called compile time polymorphism and also called early binding.
- Early banding means that you are already bound to the function call and you known that this function is going to run.
- Let's see an example of Compile time Polymorphism.
- There are two types Function Overloading and Operator Overloading.
A. Function Overloading
- The function with same name and different parameter is called function overloading
// Example 1: Find the Geometry Formula
class Geometry
{
void area(int height,int width)
{
int ar=height*width;
System.out.println("Area of Rectangle="+ar);
}
void area(int side)
{
int ar=side*side;
System.out.println("Area of Square="+ar);
}
void area(float r)
{
float ar=3.14f*r*r;
System.out.println("Area of Circle="+ar);
}
void area(float base,float height)
{
float ar=0.5f*base*height;
System.out.println("Area of Triangle="+ar);
}
}
class Easy
{
public static void main(String[] args)
{
//Creating instance(object)
Geometry obj=new Geometry();
//single para for square
obj.area(12);
//double int para for rectangle
obj.area(5,6);
//single float for circle
obj.area(2.2f);
//double float for trianlge
obj.area(2.5f,6.3f);
}
}
Example Explanation :
Here Geometry is class name contains two function with same name area() with different paramater,first with two parameter height and width and second with single parameter side .Therefor when two integer parameter is passed at the time of calling Rectangle area will be calculated, when single integer parameter is passed Square area will be calculated
Output :
Area of Square=144
Area of Rectangle=30
Area of Circle=15.197601
Area of Triangle=7.875
Area of Square=144
Area of Rectangle=30
Area of Circle=15.197601
Area of Triangle=7.875
Example 2: Find Integers and Characters with Function Overloading
class Overload{
public static int sum(int a, int b){
return a+b;
}
public static char sum(char s, char j){
return s+j;
}
public static void main(String args[]){
// System.out.println(sum("Shailesh", "Priya"));
// Can't Sum because string cannot be converted to int
System.out.println(sum(4, 5));
}
}
B. Operator Overloading
- Operator overloading is used to overload or redefines most of the operator available in Java.
- For Example:
- We can overload the operator "+" and defines its functionality to add two strings.
- Operator loading is also an example of compile time polymorphism because the compiler already known at the compile time which operator is perform the task.
- Operator Overloading Cannot overload are following : -
- Scope Operator (::)
- Sizeof()
- member selector(.)
- member pointer selector(*)
- Ternary Operator(? ;)
Example of Operator Overloading
2. Runtime Polymorphism :
- Function with same name and same parameter is called function overriding.
- It is not possible to make two function with same name and same parameter in a single class therefor to implement function overriding derived class is used.
- Let's see an example of Function Overriding
//Function Overriding
class Geometry1
{
void area(int height,int width)
{
int ar=height*width;
System.out.println("Area of Rectangle1="+ar);
}
}
class Geometry2 extends Geometry1
{
void area(int height,int width)
{
int ar=height*width;
System.out.println("Area of Rectangle2="+ar);
}
}
class Easy
{
public static void main(String[] args)
{
//Creating instance(object)
Geometry2 obj=new Geometry2();
//calling function
obj.area(12,13);
}
}
Outputs :
Area of Rectangle2=156
Comments