Interview Questions : Runtime vs static polymorphism

1. Static polymorphism occurs in compile time like method overloading.
2. Dynamic polymorphism occurs in run time like method overriding.
/*
1. Runtime polymorphism(different Class)
*/
class X {    
   public String overRidedMethod(){
      return "From Super Class";
   }
}

class Y extends X {

   public String overRidedMethod() {
      return "From Sub Class";
   }
}

public class Main{ 
 //Satic polymorphism(same class)
  public String testMethodSum(){
 return "Method overloading/Static Binding";
  }


  public String testMethodSum(String s){
 return s;
  }

  public static void main(String args[]) {  

   Main main = new Main();
   System.out.println(main.testMethodSum());
    X a = new X(); 
         X b = new Y(); 

      System.out.println(a.overRidedMethod());

      System.out.println(b.overRidedMethod());    
  }  
}

Reactions

Post a Comment

0 Comments