Thursday, November 12, 2015

Polymorphism in JAVA with Example

Polymorphism:

Polymorphism is the idea that an object can behave as many different types. 
Polymorphisms is nothing but ability to take more than one forms(One               Interface,Multiple Methods or One Name,Many Forms).

Let us take one real time example to clear our confusion regarding definition of polymorphism.                          

It looks(by the name in programming) like same but expresses different characters. These twin brothers looks alike but they hold different characters.
so, polymorphism allows you define one interface and have multiple implementations.




Why Polymorphism is done in OOP?

  • we do inheritance between two classes, all the methods and properties of the first class are derived to the other class so that this becomes the child class which  adobes all the functionality of base class.It can also possesses its own separate methods.    
  • But there is a big problem in inheriting the second class to the first class as it adobes all the methods same as the base class has,which  means that after inheritance both(base class& child class) have the methods of same name and same body

Following concepts demonstrate different types of polymorphism in java:

1) Method Overloading
2) Method Over riding

Method Overloading in java :

Deifination:   If a class have multiple methods by same name but different parameters, it is known as Method Overloading.

Advantages of Method Overloading:


Method overloading increases the readability of the program.        
How to increases the readability of the program? Explanation !

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behaviour of the method because its name differs. So, we perform method overloading to figure out the program quickly.

There are two ways to overload the method in java:

a) By changing no of arguments
b) By changing the datatype

a) Example for By changing no of arguments :

class Calculation 
{  
void sum(int a,int b){
System.out.println(a+b);
}  
void sum(int a,int b,int c){
System.out.println(a+b+c);}  
  
public static void main(String args[]){  
Calculation obj=new Calculation();  
obj.sum(10,10,10);  
obj.sum(20,20);  
}  
}  

Output:
30
40

b) Example for By changing data type:

class Calculation1 
{  
void sum(int a,int b)
{
System.out.println(a+b);
}  
void sum(double a,double b)
{
System.out.println(a+b);
}  
public static void main(String args[]){  
Calculation obj=new Calculation();  
obj.sum(10.5,10.5);  
obj.sum(20,20);  
}  
}  

Output:
 21.0
 40

Method Overriding in java: 

What ever the parent has by default available to the child class through inheritance, If the child class is not satisfied with the parent class implementation then the child is allowed to overwrite that parent class method to provide it’s own specific implementation, this concept is nothing but “overriding”.

Usage of Java Method Overriding

a) Method overriding is used to provide specific implementation of a method that is already       provided by its super class.

b)Method overriding is used for runtime polymorphism

Rules for java method overriding: 

  • method must have same name as in the parent class
  • method must have same parameter as in the parent class.
  •  must be IS-A relationship (inheritance)

Real Example for method overriding in java:

Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 7%, 8% and 9% rate of interest.

EXAMPLE:

class Bank{  
int getRateOfInterest(){
return 0;}  
}  
class SBI extends Bank{  
int getRateOfInterest()
{
 return 7;}  
}  
class ICICI extends Bank{  
int getRateOfInterest(){
return 8;}  
}  
class AXIS extends Bank{  
int getRateOfInterest(){
return 9;}  
}  
class Test2{  
public static void main(String args[]){  
SBI s=new SBI();  
ICICI i=new ICICI();  
AXIS a=new AXIS();  
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());  
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());  
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());  
}  
}  

Output:

SBI Rate of Interest: 7
ICICI Rate of Interest: 8
AXIS Rate of Interest: 9

2 comments:

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...