Static And Dynamic Polymorphism

How to achieve Dynamic or Static polymorphism in Java?

Polymorphism refers to the ability of an object to behave differently to the same message.

Polymorphism is of two types. static or dynamic. In dynamic polymorphism the response to message is decided on run-time while in static polymorphism it is decided on compile-time.

The assignment of data types in dynamic polymorphism is known as late or dynamic binding. In dynamic binding method call occur based on the object (instance) type at Run time. Eg: method overriding

If the assignment of data types is in compile time it is known as early or static binding. In static binding method call occur based on the reference type at compile time. Eg: method overloading

Method Overloading - This means creating a new method with the same name and different signature. It uses early binding.

Method Overriding - This is the process of giving a new definition for an existing method in its child class. All object created at run time on the heap therefore actual binding is done at the runtime only.

Method overloading would be an example of static polymorphism, whereas overriding would be an example of dynamic polymorphism. And Method overloading would be achieved as follows:

Code:

/*Method Overloading*/
class Example{

void Method1(){}
void Method1(Int i,j){}
void method1(Int i,char c){}

}

void main()
{
 Method1 method=new Method1;
method.Method1();
method.Method1(5,6);
method.Method1(7,'A');
//it will automatically calls the proper method by checking the variables declared in the method call.
}
 

/*Method overriding*/

class Method1{
void Method1(){}
}

class Method2{
double method2(int i,char a){}
}

void main()
{
Method1 method=New Method1;
method.Method1();
}

---

Method overloading and overriding both can be done in Static and dynamic polymorphism.

It all depends on when the right method selection is being done. i.e., it depends on the type of method we use for overloading and overriding.

Method overloading and overriding using Instance methods come under runtime/dynamic polymorphism

Method overloading and overriding using static/private/final methods come under compile time/static polymorphism

Please remember overriding is not possible using private and final methods.

Please find the example below proving that method overriding is an example of Dynamic polymorphism.

//verify overriding using Instance methods
//Prove that overriding is an example of dynamic polymorphism also.
 

class Sample
{

void display()
{

System.out.println("\nSample class display() method");

}

}
 

class Demo extends Sample
{

void display1()
{

System.out.println("\nDemo classs display() method");

}


 
 

class Poly2
{

public static void main(String args[])
{

Demo d = new Demo();

d.display(); 

}

}
 

/*

In the above program sample class display method will be called.
if the display1() method in Demo class is renamed as display(), then Demo class display method will be called.

It means, if a method is called using subclass object, JVM searches for the method in both subclass and super class.
If the method is available both in super class and subclass (i.e., if the method is overridden), then the sub class method is executed.

Thus overriding is also done dynamically.

Hence, Method Overriding is not only Static polymorphism but also a form of dynamic polymorphism. So it all depends on the type of method used for overloading as explained in the begining.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.