Polymorphism In Object Oriented Programming

  • By
  • February 9, 2021
  • JAVA

Polymorphism is one of the most important features in Object-Oriented Programming Languages like Java, C#.Net, Python, C++,..etc. 

Definition: Polymorphism is a process of representing one form in multiple forms, Polymorphism is retrieved from the Greek language, The word/term “poly” means many and the “morphs” means forms. So finally polymorphism means many forms.

Polymorphism is not only an object-oriented programming system concept but also, it is one of the principles of OOPS(others are 1. Abstraction, 2. Encapsulation, 3. Inheritance). This feature of objects oriented programming language is common but its implementation varies from one objects oriented programming language to another object-oriented programming language like Java, Python, and CPP. 

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

Real-time example of polymorphism:

Suppose let us take an example,

  1. If a person is in the classroom that time that person behave like a student,
  2. When a person is in the supermarket at that time person behave like a customer,
  3. When a person at his/her home at that time the person behaves like a son or daughter to her/his parents, the same person present in different-different places with different roles.

Generally, Object-Oriented Programming Languages Contains two types of polymorphism,

  1. Compile Time Polymorphism or Static Polymorphism
  2. Runtime Polymorphism or Dynamic Polymorphism

Note: 

Compile Time Polymorphism or Static Polymorphism we can achieve by using a concept called method, constructor, and operator overloading.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

Runtime Polymorphism or Dynamic Polymorphism we can achieve by using a concept called method overriding.

Compile Time Polymorphism or Static Polymorphism: The concept of binding or taking the overloaded method within an object at compile time is known as Compile Time Polymorphism or Static polymorphism because of static polymorphism utility of resources (RAM Memory) is poor because for each overloaded method a memory space is created at compile-time when it binds with an object. 

But in CPP environment the same problem can be solved by using dynamic or runtime polymorphism by implementing with virtual or pure virtual functions so most of the CPP developer in real worlds follows dynamic polymorphism mostly. 

Compile Time Polymorphism or Static polymorphism example in java programming,

  1. By using the method:

//package name

package com. methods;

 

//class name

public class Example2

{

 

            //product method without parameters

            public static void product()

            {

            int length = 30;

            int breadth = 10;

            int area = length*breadth;

            System.out.println(area);

              }

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

              //product method with parameters

              public static void product(int num1, int num2)

              {

            int result;

            result = num1*num2;

            System.out.println(“The Result: “+result);

            }

            //main() method 

            public static void main(String[] args) 

            {

 

            //calling product() method without parameters

            product();

            System.out.println(“===============”);

            //calling product() method with parameters

            product(20, 30);//here 20, 30 are arguments 

 

          }

 

}

 

Method overloading: In a class having same method name with different parameters is called method overloading.

 

From the above java program, the class Example2 contains method name called product() with no parameters and another one is with parameters,

 

When we call these two method in main() method, the same method will generate different values, this concept is called Compile Time Polymorphism or Static polymorphism.

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

  1. By using constructor:

//user defined package name

package com.employee.details;

 

//user defined class name

public class Employee 

{

//variables or data

private int employeeId;

private String employeeName;

private double employeeSalary;

private String employeeAddress;

//initialization of blocks

//default or zero-parameterized constructor

public Employee() 

{

employeeId = 3030;

employeeName = “Swarupa Patil”;

employeeSalary = 69585.50;

employeeAddress = “Shivaji Nagar, Pune”;

}

//parameterized constructor

public Employee(int employeeId, String employeeName, double employeeSalary, String employeeAddress

{

this.employeeId = employeeId;

this.employeeName = employeeName;

this.employeeSalary = employeeSalary;

this.employeeAddress = employeeAddress;

}

 

//Service method

public void getEmployee() 

{

System.out.println(“Employee Id: “+employeeId);

System.out.println(“Employee Name: “+employeeName);

System.out.println(“Employee Salary: “+employeeSalary);

System.out.println(“Employee Address: “+employeeAddress);

 

}

public static void main(String[] args

{

//Employee object with default constructor

Employee employee1 = new Employee();

employee1.getEmployee();

System.out.println(“—————————————“);

Employee employee2 = new Employee();

employee2.getEmployee();

System.out.println(“=======================================”);

//Employee object with parameterized constructor

Employee employee3 = new Employee(2121, “Ajay Kumar”, 60543.40, “Nampally, Hyderabad”);

employee3.getEmployee();

System.out.println(“—————————————“);

Employee employee4 = new Employee(5656, “Rajesh Reddy”, 54343.40, “SR Nagar, Hyderabad”);

employee4.getEmployee();

}

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

}

 

By Observing,

From the above java example program, in Employee we are declared two constructors, one without parameter and another one is with parameter,

when we create initialize employee objects with default constructor we will get similar or static values and when we create employee objects with parameterized constructor we will get dynamic or different values for each object.

Runtime Polymorphism or Dynamic Polymorphism:

In Dynamic polymorphism method of the class in java program binds with an object at runtime, the main advantage of dynamic polymorphism is allocating the memory space for the method at run time. 

Advantages of Runtime Polymorphism:

  • The Dynamic Polymorphism allows us to support overriding or same syntax of methods from one class to another class which is central for run-time polymorphism.

  • It allows a class to specify methods that will be common to all of its derived classes or sub classes while allowing sub classes to do the specific implementation of some or all of those methods from super classes.

  • Runtime polymorphism or Dynamic polymorphism also allows us sub classes to add its specific methods sub classes to define the specific implementation.

  1. Less memory space required
  2. Less execution time need
  3. More performance we gain

Dynamic Polymorphism or Runtime polymorphism we can achieve with the help of Method Overriding,

Super Class Address.java

//user defined package name

package com.employee.details;

 

//user defined class name

public class Address 

{

//variables

private String city;

private String state;

private String country;

//constructors

public Address() 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

{

city = “Hyderabad”;

state = “Telangana State”;

country = “India”;

}

//method of Address class

public void getData()

{

System.out.println(“Employee City: “+city);

System.out.println(“Employee State: “+state);

System.out.println(“Employee Country: “+country);

}

 

}

 

Sub Class Employee.java

 

//user defined package name

package com.employee.details;

 

//user defined class name

public class Employee extends Address

{

//variables or data

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

private int employeeId;

private String employeeName;

private double employeeSalary;

//initialization of blocks

//default or zero-parameterized constructor

public Employee() 

{

employeeId = 3030;

employeeName = “Swarupa Patil”;

employeeSalary = 69585.50;

}

//Overridden method from Address class

public void getData() 

{

System.out.println(“Employee Id: “+employeeId);

System.out.println(“Employee Name: “+employeeName);

System.out.println(“Employee Salary: “+employeeSalary);

 

}

public static void main(String[] args

{

Employee employee = new Employee();

employee.getData();

System.out.println(“===========================”);

Address address = new Address();

address.getData();

}

}

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

If we observe the above programs, in employee class we created two objects with Address class and Employee class, with these class object references we are calling getData() same method, which method will behave differently in two classes.

 

Conclusion: The main reason behind using polymorphism in object-oriented programming languages to gain different behavior or implementation.

 

Call the Trainer and Book your free demo Class for JAVA now!!!

© Copyright 2019 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*