Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Dynamic and Static Polymorphism in Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-11-2008
Member
 
Join Date: Nov 2008
Posts: 27
Dynamic and Static Polymorphism in Java
  

Hi,

Please suggest, How to achieve Dynamic or Static polymorphism in Java, if possible explain with example.

Thanks

Reply With Quote
  #2  
Old 12-11-2008
Member
 
Join Date: Oct 2008
Posts: 71
Re: Dynamic and Static Polymorphism in Java

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:
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();
}
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.
Reply With Quote
  #3  
Old 12-11-2008
Member
 
Join Date: Feb 2008
Posts: 129
Re: Dynamic and 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
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
Reply With Quote
  #4  
Old 09-06-2010
eswaraaa
 
Posts: n/a
Re: Dynamic and Static Polymorphism in Java

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');
}
}
Reply With Quote
  #5  
Old 19-08-2010
Member
 
Join Date: Aug 2010
Posts: 1
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Dynamic and Static Polymorphism in Java"
Thread Thread Starter Forum Replies Last Post
Inheritance and polymorphism in static method Vodka Software Development 5 04-03-2010 10:46 AM
Static and Dynamic RAM Harpreet Gaur Motherboard Processor & RAM 5 29-01-2010 01:31 PM
Is java supports polymorphism? beelow Software Development 3 19-11-2009 10:30 AM
Combination of static libraries in a dynamic Badrunath Software Development 3 27-10-2009 06:48 PM
Run - Time polymorphism with Overriden method in JAVA NAYASA Software Development 1 04-12-2008 06:11 PM


All times are GMT +5.5. The time now is 09:33 AM.