Wednesday, 11 August 2021

Polymorphism in java

Polymorphism means same name with different behavior and different implementation. Basically it related to multiple method declaration with same name & different behavior or functionality. It may happen in same class or child & parent class both.

In Java, we can achieve polymorphism in two ways :-

1) Method overloading

2) Method overriding.

Method overloading 

When we declared same method name with different prototype (different parameters) for achieve multiple task, it is called as method overloading. It may happen in same class or parent and child class.

Example 

public class Student{
    public String enroll(String DOB){
        String sRegNo = "";
        //business logic to enroll as a student in db and get sRegNo
        System.out.println("Student Registration done based on DOB");
        return sRegNo;
    }
     public String enroll(String DOB, String sPassYear){
        String sRegNo = "";
        //business logic to enroll as a student in db and get sRegNo
        System.out.println("Student Registration done based on DOB &  sPassYear");
        return sRegNo;
    }
}
public class Register{
    public static void main(String []args){
        String sRegNo = "";
        Student st = new Student();
        sRegNo = st.enroll("10/10/2002");
        sRegNo = st.enroll("10/10/2002", "2020");
    }
}

It is also known as Compile time polymorphism or Static binding or early binding. Because during compile time Java Compiler will decide which method need to call in Register class.

Method overriding 

When we declared same method name with same prototype (same parameters) for achieve multiple task in child class which is already declared in parent class, it is called as method overriding. It is possible in child class only. Here child class method will overwrite/override the functionality defined in parent class for same method.

Example 

public class Batch2020{
    public String enroll(String DOB){
        String sRegNo = "";
        //business logic to enroll as a student in db and get sRegNo
        System.out.println("Student Registration done based on DOB");
        return sRegNo;
    }
}
public class Student extends Batch2020{
    public String enroll(String DOB){
        String sRegNo = "";
        int iAge=0;
        //business logic to calculate age and store in iAge variable
        if(iAge>25){
            //business logic to enroll as a student in db and get sRegNo
            System.out.println("Student Registration done based on DOB");
        }else{
            sRegNo = null;
        }
        return sRegNo;
    }
}
public class Register{
    public static void main(String []args){
        String sRegNo = "";
        Student st = new Student();
        sRegNo = st.enroll("10/10/2002");
        sRegNo = st.enroll("10/10/1950");
    }
}

It is also known as Run time polymorphism or Dynamic binding or Late binding. Because during run time only JVM will decide which method need to call either Student or Batch2020 class in Register.

No comments:

Post a Comment

Please share your feedback