Results 1 to 5 of 5

Thread: Dynamic and Static Polymorphism in Java

  1. #1
    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

  2. #2
    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.

  3. #3
    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

  4. #4
    eswaraaa Guest

    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');
    }
    }

  5. #5
    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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •