You must Sign In to post a response.
  • Category: General

    What is runtime polymorphism

    What is runtime pilymorphism

    if I decided by adding if condition which function to call e.g

    if true then
    abc("abc")
    else
    abc(1)
    end if

    is this a runtime polymorphism if no then please explain.
  • #755010
    run time polymorphism is implemented at runtime for example: method overriding. If the method in the base class is a virtual method it can be overriden in the child class.

    The methods you are calling in your code are called overloaded methods as they differ by method parameters whereas the method name remains the same so at compile time, which method has to be called is known by the compiler.

    In your below code, method abc is called with different parameter types, the string parameter type and the integer type.

    abc("abc")
    else
    abc(1)

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #755017
    In my case also compiler doesn't know which function to call one which takes string as parameter or integer. it is also decided on the basis of if condition at runtime.

    and what u said it called as function overriding where base class and derived class function name and signature should same.

    polymorphism itself says same name but diff signature .

  • #755027
    Hi Jevan,

    Runtime polymorphism nothing but method overriding. or u can called as "Late Binding".

    here method overriding means same method name with same signeture(i.e parameter) that can overrided by child class but that is not compulsory for child class(may be override or may not be).

    why its called runtime polymorphism. in runtime only we will send the parameter to overrided method which is in child class according to the inputs we will get the outout.

    For an examples:

    class Shape
    {
    public virtual void Draw()
    {

    }
    }

    class Ractangel:Shape
    {
    public override void Draw()
    {
    Console.WriteLine("Rectangle Drawn ");
    }

    }

    class Circle:Shape
    {
    public override void Draw()
    {
    Console.WriteLine("Circle Drawn ");

    }

    }
    class Traingle:Shape
    {
    public override void Draw()
    {
    Console.WriteLine("Triangle Drawn ");

    }
    }

    static void Main(string[] args)
    {

    Console.WriteLine("\n\nRuntime polymorphism test\n\n");

    Circle objcircle = new Circle();
    objcircle .Draw()
    Ractangel objrectangle = new Ractangel();
    objrectangle .Draw();
    Traingle objtrianle = new Traingle();
    objtrianle .Draw();

    Console.ReadKey();
    }


    hope its helpful u for ur understanding

    Thanks,
    CHitaranjan

  • #755040
    Run time polymorphism defined the child class functionality even though there is a base class definition is present.

    in your case you have overloaded the method which is Design time polymorphism.

    if one of your method is in base class and another is in derived class using Virtual and override keyword in that case you will have the Run time polymorphism.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #755075
    Thanks for ur reply ..

    I think if objects all created at compile time
    then at compile time only u know which fucntion to be executed

  • #755077
    Polymorphism is a process to perform a single action by different way. Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than compile-time. Example

    class Vech
    {
    void run(){System.out.println("running");}
    }
    class Biker extends Bike{
    void run(){System.out.println("running safely with 50km");}
    public static void main(String args[]){
    Bike b = new Bike();
    b.run();
    }
    }
    Reference
    http://howtodoinjava.com/2013/07/15/what-is-polymorphism-in-java/

  • #756055
    Before knowing Run-time polymorphism, let's explore 'What Polymorphism is?'

    Polymorphism (defined):
    This is the ability for different classes to provide different implementations of same method. Also, the 'polymorphism' means we can have multiple classes that can be used interchangeably to use implementation of specific method in different ways.

    There are 2 types of Polymorphism:

    > Run-time polymorphism (dynamic binding , late binding)
    and
    > Compile time polymorphism (early binding)

    1). Run-time polymorphism (Defined):
    This is also known as lazy load, late binding, dynamic binding, overriding. And this can be achieved by 4 approaches as below:

    - Method overriding ( Base class functionalities can be used by derived classes or can be overridden as needed )

    - Property overriding ( Base class properties can be used by derived classes or can be overridden as needed )

    - Using Delegate ( With delegates a polymorphic data structure can be created, by assigning desired function to delegate at run-time )

    - Using Interface ( Different classes can implement same interface in different ways )

    2). Compile-time polymorphism (Defined):

    This is also known as early binding, static binding & overloading. And this can be achieved by 3 approaches as below:

    - Method overloading ( Different implementation of same method which are differentiated with their signatures )

    - Operator overloading ( Different implementation of same operator which are differentiated with their signatures )

    - Indexer overloading ( Different implementation of an indexer which are differentiated with their signatures )

    ** Note **
    Why we have choosen only these Methods, Operators and Indexers (and not 'Properties') for 'overloading'? Because, they receive parameters, based on which there can be multiple overloads created for them. Whereas, a Property cannot be overeloaded, as it doesn't receive parameters.

    Hope, this helped.
    Regards

  • #757322
    If two methods have the same method name and different numbers of parameters or different (data) types of parameters then it is run-time polymorphism if the method parameters are specified at run-time depending on values passed. It is also called run-time binding.

  • #768682
    Runtime Polymorphism is the polymorphism existed at runtime. In Runtime Polymorphism, call is not resolved by the compiler.It is achieved by virtual functions and pointers.It is also known as Dynamic Binding, Late Binding and overriding as well.Overriding is runtime polymorphism having same method with same parameters or signature, but associated in a class and it's sub-class.It is more flexible as all things executed at runtime and also provides slow execution as compare to early binding.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.