Subscribe Us

Polymorphism in Java With Example

.
Polymorphism in Java
Ploymorphism in java

Polymorphism means the ability of object to appear in many forms . polymorhism derived from 2 greek word Poly : many and mrophs : form 



Types of Polymorphism 
Polymorphism in java have 2 sub part.

1)  Compile time polymorphism
2) Run time Polymorphism




1) Compile Time Polymorphism 
Compile time polymorphism is also know as the static polymorphism .compile time polymorphism is achieved by method overloading .

Method Overloading Compile Time Polymorphism 

When a class contain multiple method with same name but different number of argument then is called method overloading .overloading is also known as the compile time polymorphism . 

Syntax of method overloading


Example - same method name but different number of argument 
class Addition

void add (int a){}
void add (int a , int b){}
 

Example - Same method name and same number of argument but different data types.
 class Addition

{  
void add (int a){}
void add (char c{}
 

Way to overload the method 
There are 2 way to overload a method in java programming.
1) By changing the number of parameter.
2) By changing the data type of parameter.

Method overloading example - By Changing the number of parameter.


class Addition

void add (int a)
{
System.out.println(a);
}
void add (int a , int b)
{
System.out.println(a+b);
}
 
class Test 
{  
public static void main(String args[])
{  
Addition s=new Addition(12);
Addition s=new Addition(12,10);
}
}

Output : 
12
22

Method Overloading Example : By changing the data type of parameter.

class Addition

void add (int a)
{
System.out.println(a);
}
void add (char ch)
{
System.out.println(ch);
}
 
class Test 
{  
public static void main(String args[])
{  
Addition s=new Addition(12);
Addition s=new Addition('S');
}
}

Output :
12
S

2) Run Time Polymorphism 
Runtime Polymorphism is also know as the dynamic binding or late binding .Run time polymorphism is achieved by method overriding. 

Method Overriding - Run Time Polymorphism

When a child class (sub class ) has the same method name as declared in parent class then those method are called as the override method . In overriding parent class method is called as " overridden method"
and child class method is called as the "overriding method ".

Example of method overriding 

class Teacher
{  
void exam()
{
System.out.println("Exam will on next monday");
}  
}  

class Student extends Teacher
{  
 void exam()
{
System.out.println("Chang the Exam schedule");
}  
public static void main(String args[])
{  
Student std = new Student();  
std.exam(); 
}  
}  
Output :
Change the exam schedule

Method Overriding Rules
There are some rules which must be follow while overriding .

  • Method signature (parameter list) of both parent and child class must be same .
  • Method name of both parent and child class must be same .
  • The return type of child method and parent method must be same as primitive level.

Hope !! The above Lesson of polymorphism in java helpful for you ...

Thank You !
Team ,Java Acharya 

Tags : Java polymorphism , types of polymorphism , runtime polymorphism , compile time ploymorphism , example of polymorphism 

Post a comment

0 Comments