Ad Code

Responsive Advertisement

Runtime polymorphism

Runtime Polymorphism

Runtime Polymorphism


Polymorphism:

  • Poly - Many, Morph - Shapes

     Process by which code, method or object behaves differently under different Circumstances.

REAL TIME EXAMPLE:

        A person can play many roles in his\her life.

Lets take women as an example,

           She plays the role of

              Daughter for her parents,

              Student in her school, 

              Mother for her children etc..

Types of Polymorphisms:

                Ã˜ Compile Time Polymorphism

             Ã˜ Run Time Polymorphism


Runtime Polymorphism

ü It is also called Dynamic Polymorphism.

ü It happens at runtime

ü The value or shape for the object is decided during runtime or execution time.

ü Runtime polymorphism is achieved through method overriding.

Overriding:

ü It is the runtime polymorphism feature in which entity has the same name but its implementation changes during execution.

Method Overriding:

ü Method overriding occurs at runtime polymorphism.

ü When the Super class and Sub class includes the method that has the same method name and parameter i.e same method signature is called method overriding.

Real Time Example:

         class vijay{

               void transport(){

               System.out.println("I am Vijay, I have bike ");

                }

        public class Sanjay extends vijay{

             System.out.println("I am Sanjay, I have car ");

              }

           public static void main(String[] args) {

                 vijay obj1 = new vijay();

                 obj1.transport()//Here the parent class method gets called.

                 vijay obj2 = new Sanjay();

                obj2.Sanjay(); //child class method gets called

                     }

              }

Output:

       I am Vijjay, I have bike

       I am Sanjay, I have car

Here, vijay is the Super class who  has the method transport and Sanjay is the child class it also has the same method transport as the Super class but its implementation varies.

ü Languages that do not support method overriding : C.

ü Languages that support method overriding : C++, Java, Python.


Post a Comment

0 Comments

Close Menu