Polymorphism Part 1 (Compile Time Polymorphism)

                                                                               Facebook - Free social media icons https://www.facebook.com/techaddaa/ 


Hello Friends, 

This is second post in core java concepts blog. This series is specially designed for beginners to help them in their learning.

Introduction - 

There are many concepts like Classes, Object,  Polymorphism, Inheritance etc. Here we will explore polymorphism and learn types of polymorphism, it s'advantages along with implementation in java language.


                      poly2  


Prerequisite -


What is Polymorphism?

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages.This principle can also be applied to object-oriented programming and languages like the Java.

Polymorphism is implemented in two ways -

  1. Compile time polymorphism
  2. Run time polymorphism 


1. Compile time polymorphism is also known as static polymorphism because this behaviour can be determine or predict at the time of program compilation. This is achieved with the help of function overloading in java.

Function/Method overloading is flexibility where we can have same name for multiple functions/methods. Compiler determines function calling on the basis of no of arguments, type of arguments.

Here we have three overloaded methods where name is same "add" but ultimately which method will be invoked that will depend on method arguments.

public void add(int a, int b);   - this method will add two numbers 

public void add(int a, int b, int c); - this method will add three numbers 

public void add(String a, String b); - - this method will add two strings


P.S. (***) - 

Method return type does not impact overloading behaviour. Below two methods are not case of overloading and this will give compile time error.

public int add(int a, int b);  

public void add(int a, int b);  

 

To understand compile time polymorphism in details, lets implement and understand. if we know how to implement concept in program then you have better understanding than if you know only theory. We will create class explained in above example. 

Define the class "Calc". Template/blueprint.

package com.utility;

public class Calc {

public void add(int a, int b) { // this method will add two numbers
System.out.println("a+b = " + a+b );
}

public void add(int a, int b, int c) { // this method will add three numbers
System.out.println("a+b+c = " + a+b+c );
}
public void add(String a, String b) { // this method will add two strings
System.out.println("a +b = " + a.concat(b) );
}

}

We have learned define class and object creation for that class in last post. So lets create object of Calc class. With that object we will call overloaded method add.

Create one more class for demo. - PolymorphismDemo.java

package com.demo;

import com.utility.Calc;

public class PolymorphismDemo {


public static void main(String[] args) {

Calc calc = new Calc();

calc.add(5,6);
calc.add(2,3,5);
calc.add("Compile Time"," Polymorphism");

}

}


Run the Above Class--

Output - 

a+b = 56
a+b+c = 235
a +b = Compile Time Polymorphism

Process finished with exit code 0


Source code download - 
https://github.com/sarveshoza/PolymorphismInJava/tree/compiletimepolymorphism

Advantages-

1. It allows programmers to provide same interface/ method name to call. behaviour will determine on the basis of  calling parameters.
2. Programmers have flexibility to define same name for multiple similar methods.
3. Polymorphism helps to remove unnecessary complexity and hide unwanted details from users.User just need to know the method signature.
4. Increase code re-usability and minimise code maintenance.

There are also programmers who thinks polymorphism reduces code readability and it take more time in design phase. Agree to some extents with them but personally for new beginners, it could bit tough to understand existing overloaded code but expert programmers use this feature to provide same experience to end users for similar operations and i find this useful in my career. 

2. Runtime Polymorphism-
To understand runtime polymorphism or method overriding, first we need to understand one more important concept that is "Inheritance". So we will cover runtime polymorphism in part 2 of this post in upcoming weeks. 

Bonus-

In this section, we will see how return type does not play any role in method overriding.

Let's modify our Calc class and add one more method in that class.

public int add(int a, int b){
return (a+b);
}


We will see compilation error. It means if two methods have same signature but different return type then those method would not qualified as overloaded method.

Error:(16, 16) java: method add(int,int) is already defined in class com.utility.Calc


Till here, we have learned -
1. Create class
2. Create Object
3. Define methods 
4. Overload methods
5. Method Calling


Upcoming Posts - 

We will have more fun and learning in upcoming weeks. Bookmark this space in your favourites. 

1. Inheritance 
2. Runtime Polymorphism - Part 2


You can also suggest your favourite topics in comments section or on Facebook page. We will try to start exploring on those topic. You can also suggest points to make blogs more effective.

To connect more and discuss technology, Please visit and like the page. We will list all the posts there and provide updates about upcoming events/posts.

Comments

Popular posts from this blog

Inheritance - Object-oriented programming (Parent-Child)

Class & Object in java - Do you know ?????