Polymorphism

Polymorphism is an ability of an object to take on many forms.
There are two types of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).
Polymorphism provide dynamic behavior.


Compile time polymorphism (method overloading)-

If two or more method in a class have same name but different parameters, it is known as method overloading.
Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing number of arguments or by changing the data type of arguments. If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method.


Code snippet –
/**
 * Method overloading
 * @author Manoj
 */
class TestOverloading {
      
   public int sum (int num1, int num2) {
       return num1 + num2;
   }
      
   public int sum (int num1, int num2, int num3) {
       return num1 + num2;
   }
}


Runtime time polymorphism (method overriding)-

If we declare parent class method into child class with same name and same no and type of arguments, this behavior is called as method overriding.
The return type of overridden method must be same or it could be covariant or subtype of parent’s method return type.
The access modifier of child method could be less restrictive means if super class access modifier is public than child class method couldn’t be private or protected but super class method is private than child method could be public or protected.
Method declare as private or static can’t be overridden.
Overridden method can’t thow broader or checked exception
Constructor can’t be overridden.

Code snippet –
/**
 * Method overriding
 * @author Manoj
 */
class Parent {
  public String getMethodOne(int numberOne){
       return "Parent MethodOne";
  }
      
  protected String getMethodTwo(int numberTwo) throws Exception{
      return "Parent MethodTwo";
  }
}
class Child extends Parent {
  //if we declare it as private or protected
  //it will give compile time error(cann't reduce the visibilty
  //of inherited method from parent
  public String getMethodOne(int numberOne){
       return "Child MethodOne";
  }
       //can reduce or eliminate the exception
       //but can't throw broader or new checked exception
       //otherwise it will give compile time error
       //exception is not compatible with parent methods
 public String getMethodTwo(int numberTwo){
       return "Parent MethodTwo";
   }
}




class ExceptionTest {
     public void testUncheckedException() {
     }
     public void testCheckedException() {
     }
     public void testBroaderException() throws IOException{
     }
     public void testNarrowingException() throws Exception{
     }
}
public class TestMain extends ExceptionTest {
     public void testUncheckedException() throws ArithmeticException {
      //Can throw unchecked exception
     }
     public void testCheckedException() throws Exception {
      //compiler exception can't throw checked exception.
     }
     public void testBroaderException() throws Exception{
      //Compiler exception for broader exception
     }
     public void testNarrowingException() throws IOException{
      // Narrowing the exception
     }
}



Access Modifier change in Overriding
class ExceptionTest {
       private void privateMethod() {
              // not visible to override
       }
       void defaultMethod() {
       }
       protected void protectedMethod() {
       }
       public void publicMethod() {
       }
}

public class TestMain extends ExceptionTest {
       public void defaultMethod() {
              // default method can be default, protected & public
              // private modifier will give compile time error
       }
       public void protectedMethod() {
              // default method can be protected & public
              // private and default modifier will give compile time error
       }
       public void publicMethod() {
              // default method can be public only
              // private, protected and default modifier will give compile time error
       }
}


1 comment: