OOPs Concept - 3


What is Polymorphism?

Polymorphism is one of the important principle of OOPs. In this post we are going to discuss about Polymorphism. This word has been derived from two words poly (which means many) and morph (which means forms), so Polymorphism means many forms. In OOP an object can exists in more than one form, which can happen when we have many classes related to each other through Inheritance. This can  be achieved when a parent class reference is used to refer a child class object.

Let’s take our example from previous post on Inheritance, we have a super class Employee and two sub classes PartTimeEmploye and FullTimeEmployee.

class Employee{
      private String empName;
      private String empId;


      public void storeDetails(){}
      public void showDetails(){}
}

class PartTimeEmployee extends Employee{
      private void calculateSalary(){}
}

class FullTimeEmployee extends Employee{
      private void calculateSalary(){}
}


Now let’s create a main class for using these classes :

class MainClass{

  public static void main(String []args){
     Employee emp1 = new Employee();
     Employee emp2 = new PartTimeEmployee();
     Employee emp3 = new FullTimeEmployee();
  }

}


So in above main class you can see we have created three instance of Employee class emp1, emp2 and emp3. In above example emp1 is purely an Employee, emp2 is PartTimeEmployee and emp3 is FullTimeEmployee, which shows instance of Employee class can exists in many form. This is what Polymorphism is all about.

There are two more terms Overriding and Overloading that's been considered as type of Polymorphism. Let's discuss about therm.

Overriding : When we extend parent class for creating child classes we can also override functions in parent class as per the requirement in child classes, this is know as overriding. Consider below example.

class Employee{
      private String empName;
      private String empId;


      public void storeDetails(){}
      public void showDetails(){}

      public void calculateSalary(){
        System.out.println("This is default method for calculating salary.");
      }
}

class PartTimeEmployee extends Employee{
      private void calculateSalary(){

        System.out.println("This is method for calculating salary for part time employees.");
           }
}

class MainClass{

  public static void main(String []args){
     Employee emp1 = new Employee();

     emp1.calculateSalary();

     Employee emp2 = new PartTimeEmployee();

     emp2.calculateSalary();
  }

}


We can see we have overridden method calculateSalary so when above main class is executed that will print below statements on console :

This is default method for calculating salary.
This is method for calculating salary for part time employees.

calculateSalary now has two forms one for Employee class and other for PartTimeEmployee class.

Overloading : When we have same method with different set of arguments in a class then it is known as method overloading. Below is an example of overloading :

class OverloadingExample{
 
       public void print(int a, int b){
             System.out.println("a : "+a+" , b : "+b);
       }

       public void print(String a, int b){
             System.out.println("a : "+a+" , b : "+b);
       }

       public void print(String a, String b){
             System.out.println("a : "+a+" , b : "+b);
       }   

       public static void main(String[] args){
             OverloadingExample exp = new OverloadingExample();
             exp.print(10,20);
             exp.print("ABC",20);
             exp.print("ABC","XYZ");
       }
}


In above example we can see print method has been overloaded three times, so when above program is executed we will see below output on console.

a : 10 , b : 20
a : ABC , b : 20
a : ABC , b : XYZ

print method has now more than one form and which form is executed depends on number and type of arguments passed.

This was all about polymorphism, we will discuss about abstraction in next post.
Share:

Related Posts:

No comments:

Post a Comment

Popular Posts

Recent Posts

Followers

Total Pageviews

5,030