Polymorphism explained simply!

Polymorphism explained simply!

OOP | For beginners | Dynamic vs. Static

The four basic concepts of OOP (Object Oriented Programming) are Inheritance, Abstraction, Polymorphism and Encapsulation. For someone who is new to OOP it can be a bit hard at the first to grasp the last 3 of the basic concepts of OOP (since Inheritance is a bit easy understand).

Polymorphism is the ability of an object to take on many forms.

Any Java object that can pass more than one IS-A test is considered to be polymorphic? tutorialspoint. This means any child class object can take any form of a class in its parent hierarchy and of course itself as well. What this actually means is that the child class object can be assigned to any class reference in its parent hierarchy and of course itself as well.

Eg: Lets say Student class is a child of Person class

Student student = new Student()Person person = new Student()

The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Actually here it is talking about Dynamic Polymorphism (which I will describe next).

There are 2 types of polymorphism which are commonly mentioned.

  1. Dynamic Polymorphism
  2. Static Polymorphism

Dynamic Polymorphism

This is also mentioned as Run-Time polymorphism, Dynamic binding, Run-Time binding, Late binding and Method overriding. Here having many forms is happening in different classes.

This is what is commonly known as so called ?polymorphism?. Let me explain it VERY SIMPLY –

Lets assume there are methods with same method signature in classes in a class hierarchy (parent-child relationships), these methods are in different forms (this is known as method overriding).

Then when an object is assigned to a class reference and when a method of the object is called, the method in the object?s class is executed. Not the method of the reference class (if the reference is a parent class).

What happens here is since the object creation is done at run-time the form of method which should be executed (the method in the object) can be only decided at run-time.

Eg: Java code

class Person { public void teach(){ System.out.println(“Person can teach”); }}class Teacher extends Person { public void teach() { System.out.println(“Teacher can teach in a school”); }}public class TestTeacher { public static void main(String args) { Person person = new Person(); //Person reference and object Person another_person = new Teacher(); //Person reference, Teacher object Teacher teacher = new Teacher(); //Teacher reference and obj. person.teach();//output: Person can teach // Here you can see Teacher object’s method is executed even- // -though the Person reference was used another_person.teach();//output: Teacher can teach in a school teacher.teach();//output: Teacher can teach in a school }}Image for post

Static Polymorphism

This is also mentioned as Compile-Time polymorphism, Static binding, Compile-Time binding, Early binding and Method overloading. Here having many forms is happening in the same class.

If I explain this SIMPLY..

This is actually method overloading. Method overloading is having more than one method with the same method name but with different arguments (return type may or may not be same). Here when calling the methods compiler compiler choose which method to call depending on the parameters passed when calling. This happens at compile-time.

Eg: Java code

class Calculator { void add(int a, int b) { System.out.println(a+b); } void add(int a, int b, int c) { System.out.println(a+b+c); }}public class Demo { public static void main(String args) { Calculator calculator = new Calculator(); // method with 2 parameters is called calculator.add(10, 20); //output: 30 // method with 3 parameters is called calculator.add(10, 20, 30); //output: 60 }}

Some people say method overloading is not truely polymorphism. But I think it can be categorize as a type of polymorphism since in method overloading there is a method in many forms.

Hope this helped in understanding polymorphism simply.

Cheeers! 😀

Image for post

Here are some useful links

  • Method overriding
  • Method overloading
  • Types of polymorphism
  • Difference between Runtime Polymorphism and Compile time Polymorphism
23