Pages

Polymorphsim

Polymorphism
Definition

The word "poly" means many and "morphs" means forms. Polymorphism is the ability of methods to behave differently based on the object calling it. (or) Polymorphism allows the same functions to at differently classes.

Types of Polymorphism 1. Compile Time Polymorphism (or) Static Polymorphism (or) Method Overloading (or) Early Binding.
2. Runtime Polymorphism (or) Dynamic Polymorphism (or) Method Overriding (or) Late Binding.

Method Overloading

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.

Method Overriding

If a sub class provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.

Rules of Method Overriding
  1. The Strictfp modifier has no effect on the rules of overriding.
  2. The Synchronized modifier has no effect on the rules of overriding.
  3. A Static method in a sub class may hide another static one in a super class and that is called hiding.
  4. Constructors cannot be overridden.
  5. Abstract methods must be overriedden by the first concreate sub class.
  6. Use the super keyword to invoke the overridden method from a sub class.
  7. The overriding methods must not throw new or broader checked exceptions.
  8. The overriding methods must not have more restrictive access modifier.
  9. The overriding method must have same return type or sub type.
  10. The overriding method must have some arguments list.
  11. Final and Static methods can not be overridden.
  12. Only, Inherited method can be overridden.
Difference Between Method Overloading and Method Overriding
Method Overloading Method Overriding
Compile Time Polymorphism can be achieved by Method Overloading. Run Time Polymorphism can be achieved by Method Overriding.
Method Overloading is performed within class. Method Overriding is performed in two classes that have IS-A relationship.
Method Overloading parameter must be different. Method Overriding parameter must be same.
Method Overloading is used to increase the readability of the program. Method Overriding is used to provide the specific implementation of the method that is already provided by its super class.
Return type can be same or different in Method Overloading. Return type must be same or covariant in Method Overriding.
While Method Overloading may or may not require inheritance. While Method Overriding always needs inheritance.

Polymorphism Example package com.jaladhi.jst;
public class AddValues {
      public static int addInteger(int firstValue, int secondValue) {
            return firstValue + secondValue;
      }
      public static addInteger(int firstValue, int secondValue, int thirdValue) {
            return firstValue + secondValue + thirdValue;
      }
      public static void main(String args [] ) {
            int totalTwo = addInteger(100, 200);
            String totalThree = addInteger(300, 500, 800);
            System.out.println("Display Total Integer Value (Two Input Values) : " + totalTwo);
            System.out.println("Display Total Integer Value (Three Input Values) : " + totalThree);
      }
}
Output :
Display Total Integer Value (Two Input Values) : 300
Display Total Integer Value (Three Input Values) : 1600

package com.jaladhi.jst;
public class AddValues {
      public static int addInteger(int firstValue, int secondValue) {
            return firstValue + secondValue;
      }
      public static addString(String firstName, String surName) {
            return firstName + surName;
      }
      public static void main(String args [] ) {
            int total = addInteger(100, 200);
            String name = addString("David", "Ping");
            System.out.println("Display Total Integer Value : " + total);
            System.out.println("Display Full Name : " + name);
      }
}
Output :
Display Total Integer Value : 300
Display Full Name : David Ping