Tuesday, June 11, 2013

Static and Dynamic polymorphism

Hi All,
Welcome to Java-recent
Today we will discuss one of the important features of Java- Static and Dynamic Polymorphism (Virtual method invocation).
Polymorphism—A core Java object oriented concept, from the name it clearly tells us representing an element in one or more forms.
There are two types of Polymorphism
  1. Static Polymorphism:- Occurs during compile time—conversion of .java to .class
  2. Dynamic Polymorphism:- Occurs during runtime—Objects creation time
We will decide in detail about them
Before going to discuss ,we need to know about Overloading and Overriding of methods

Overloading:- is the ability to define a method in different names with same name
We will put it another way,defining a method in different forms
Ex:- Suppose we have a method add(int x,int y) in class calculator,we want to do addition for float etc types also then declaring them as
Public float addInt(int x,int y){}
Public flaot addLong(long int,long y){}
Public float addFloat(flaot x,float y){} etc…

This type of declarations will not be good because all of them are intended to do similar kind of work,so here come overloading a method
Note:-
  • In Overloading methods name and return type should be same
  • Number of parameters ,order and their data type should vary
  • If Name and parameter number ,types,order are same and return value is different Java compiler will not be able to differentiate the method based on return type
public class Add {
public float add(int x,int y)
{
return (x+y);
}

public float add(int x,float y)
{return (x-y);
}
public float add(float x,float y,short z)
{return (x+y+z);
}
// Method 4
public int add(float x,float y ,short z)
{ return (x+y+z);
}
}

Here there are three methods with same name and return type with different number,type of parameters.
In method 4 though the return type is different ,compiler will not be able to differentiate.

Static Polymorphism:- is also called compile time polymorphism,its nothing but Overloading of a method
Compiler will decide which method to call during compilation process. The errors will be shown during compile time if any on method binding.

Now we will discuss about method Overriding,this will happen in IS-A relationship-Inheritance
Overriding:- In simpler terms it means we are Over-riding a method as per our needs,defining a specific functionality for a child class. Instead of definition lets move on to a example which will give us a clear idea.
Example:-
Scenario—There is a parent class called as Animal.
There are two classes called Lion and Elephant, both of them belongs to Animal group so they have a IS-A(Inheritance) relationship with Animal class.
Lets put the scenario in Java code

Animal.Java
public class Animal
{
public void aboutMe()
{
System.out.println("Hi all I am the Super class for all animals");
}
}
Lion.Java
public class Lion extends Animal
{
public void aboutMe()
{
System.out.println("Hi I am King of animals");
}

}

Elephant.java
public class Elephant {
{
System.out.println("Hi I am the largest of all the animals");
}
}


Now we will declare a Tester Class.
public class Tester
{
public static void main(String[] args) {
{
//Creation of three reference types
Animal animal,animal2,animal3;
animal=new Animal();
animal2=new Lion();
animal3=new Elephant();

animal.aboutMe();
animal2.aboutMe();
animal3.aboutMe();
}
}



Run the above code output will be as follows
Hi all I am the Super class for all animals
Hi I am King of animals
Hi I am the largest of all the animals


The method invocation is not done on reference type, it done on type of Object created.
When will be the Objects gets created? During run time—so which method need to be invoked will be decided during runtime so this is called runtime/dynamic polymorphism

Note:- Entire method signature should be same as the parent class method

Dynamic Polymorphism:- is nothing but method Overriding, method invocation is decided during runtime.
Dynamic Polymorphism is more costly than static Polymorphism because the decision making done during runtime.

Differences between Static and Dynamic Polymorphism
Static Polymorphism Dynamic Polymorphism
Method binding is done during compile time Method binding is done during runtime based on type of object
Overloading will be within same class Overriding comes into picture in IS-A relationship
Method name and return should be same.
Parameter data types, order, number should vary
Exact method signature should be followed


Happy Learning
Please provide your valuable suggestions and corrections about this article, it would be great help in producing better content.


No comments:

Post a Comment

Like and Share