Hi,
Please suggest, How to achieve Dynamic or Static polymorphism in Java, if possible explain with example.
Thanks
Hi,
Please suggest, How to achieve Dynamic or Static polymorphism in Java, if possible explain with example.
Thanks
Hi,
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:
Because in case of overloading,at compile time the compiler knows which method to link to the call.However,it is determined at runtime for dynamic polymorphism.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(); }
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
Method overloading means creating a new method with the same name and different signature. It uses early binding.
Method Overriding
Method overriding 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
Hi,
Method Overloading
Two or more methods in a Java class can have the same name, if their argument lists are different, the feature is known as Method Overloading.Argument list could differ in no of parameters,data type of parameters,sequence of parameters.
Calls to overloaded methods will be resolved during compile time.Method overloading is nothing but a Static Polymorphism.
Example:
class MethodOverloading{
public void display(int num){
System.out.println("\n\tInteger value: " + num);
}
public void display(float value){
System.out.println("\n\tFloat value: " + value);
}
public void display(char ch){
System.out.println("\n\tCharacter value: " + ch);
}
}
public class MethodOverloadingDemo{
public static void main(String args[]){
MethodOverloading ob = new MethodOverloading();
ob.display(123);
ob.display(1.11f);
ob.display('A');
}
}
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 an example in my next post 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 above post.
*/
-Balu
Bookmarks