Tuesday, 26 December 2017

Difference Between Static And Dynamic Polymorphism?

Compile time Polymorphism
The Polymorphism which occurs at compile time is called Compile time Polymorphism
Compile time polymorphism is called as static polymorphism or static binding or early binding
It provides fast execution because occurs at compile time
Example: Method overloading (Same method name and different parameter)
class Cal
{
    void add(int a, int b)
    {
         System.out.println("sum of two number:"+(a+b));
    }
    void  add(int a, int b, int c) 
    {
          System.out.println("sum of three number:"+(a+b+c));
    }
}
public class LearningPoint92
{
   public static void main(String args[])
   {
                Cal c1 = new  Cal();
                 c1.add(50, 20);
                 c1.add(50, 20, 30);
              }
        }

OUTPUT:
sum of two number:70
sum of three number:100
Runtime time Polymorphism

The Polymorphism which occurs at run time is called Run time Polymorphism
Run time polymorphism is called as dynamic polymorphism or dynamic method dispatch or dynamic binding or late binding
It provides slow execution because occurs at run time
Example: Method overriding (Same method name and same parameter)
.
class A{
   public void show(){
            System.out.println("Hello");
   }
}
public class B extends A{
        public void show(){
       //   super.show();   //to call superclass method
            System.out.println(" Welcome To LearningPoint92");
   }
   public static void main(String args[]){
            A a1 = new B();
            a1.show();
      }
     }
OUTPUT:
Welcome To LearningPoint92
Note:
upcasting is required in runtime polymorphism. Super class reference refer to subclass object is called upcasting.you can call subclass method using upcasting
A a1 = new B();
Where  A  a1=superclass reference

Where new B () =subclass reference

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...