Polymorphism in Java - Javastudypoint

Thursday, January 3, 2019

Polymorphism in Java

Polymorphism in Java is a concept which allows you to perform a single action in different ways. It is the ability of an object or method to take different forms as per requirements. Polymorphism is one of the most important features of object-oriented programming (OOPs). Polymorphism is a combination of 2 Greek words: poly and morph. The poly word means many and morphs
words means forms. So when one thing has many forms it is known as polymorphism. 



Consider a scenario where Car is a class that has a method speed()However, the speed of the car may different according to cars. for example, Maruti car has a speed of 60 Km/h, Alto car has a speed of 70 km/h, and Brezza car has a speed of 80 km/h. So here is a single method called speed() but their behavior is different according to cars. This is called polymorphism in Java.
polymorphism in java

Types of polymorphism in Java:

There are two types of polymorphism in Java: 
1. Static Polymorphism.
2. Dynamic Polymorphism.

Compile-time polymorphism is also known as static polymorphism and the runtime polymorphism is also known as dynamic polymorphism. Method Overloading is a way to implement compile-time polymorphism and the Method Overriding is a way to implement runtime polymorphism.
polymorphism in java


1. Compile-time Polymorphism.

Static polymorphism is also known as Compile-time polymorphism. Method Overloading is a way to implement compile-time polymorphism in Java.

Method Overloading in Java:

Methods are said to be overloaded when many methods in a class having the same name but different parameter list. It is similar to Constructor Overloading in Java, that allows a class to have more than one constructor having the same name but having different parameter lists. It increases the readability of the program.
Suppose we have a class Addition and you want to perform the addition of the given number, but there can be any number of parameters. If you write a method such as add2(int x, int y) for two parameters, and add3(int x, int y, int z) for three parameters, then it may be difficult for you and others to understand the behavior of the methods because of its name differs.

Ways to overload a method:

There are three ways to overload a method in Java.
1. By changing the number of parameters.
2. By changing the data type.
3. By changing the sequence of the data type.

Example of Method Overloading: changing the number of parameters.

In this example, we are going to show you how we can overload a method by changing the number of parameters. In this example, we have created two methods named as add(), first, add() method performs the addition of two numbers and the second add() method performs the addition of three numbers.

class Addition{
 public int add(int x, int y){
  return x+y;
 }
 public int add(int x, int y, int z){
  return x+y+z;
 }
}
class Person{
 public static void main(String args[]){
  Addition obj = new Addition();
  System.out.println("The Addition of two numbers is : " +obj.add(10,20));
  System.out.println("The Addition of three numbers is : " +obj.add(10,20,30));
  
 }
}

Output:
Polymorphism in Java

Example of Method Overloading: changing the data type of a parameter.

In this example, we are going to show you how we can overload a method by changing the data type of arguments. In this example, we have created two methods named as add(), But that method differs in data type. The first method calculates the addition of two integer value and the second method calculate the addition of two double value.


class Addition{
 public int add(int x, int y){
  return x+y;
 }
 public double add(double x, double y, double z){
  return x+y+z;
 }
}
class Person{
 public static void main(String args[]){
  Addition obj = new Addition();
  System.out.println("The Addition of two numbers is : " +obj.add(10,20));
  System.out.println("The Addition of three numbers is : " +obj.add(10.01, 20.02, 30.04));
  
 }
}

Output: 

Polymorphism in Java

Example of Method Overloading: changing the sequence data type.

In this example, we are going to show you how we can overload a method by changing the sequence of a data type. In this example, we have created two methods named as a display() with a different sequence of the data type of arguments list. The first method having the arguments (int, double) and the second method having the arguments (double, int).


class Addition{
 public void display(int x, double y){
  System.out.println("Defination of first method");
 }
 public void display(double x, int y){
  System.out.println("Defination of second method");
 }
}
class Person{
 public static void main(String args[]){
  Addition obj = new Addition();
  obj.display(5, 4.5);
  obj.display(2.5, 5);
  
 }
}

Output: 
Polymorphism in Java

Note: Method Overloading in Java is not possible by changing the return type of the method only because of the ambiguity problem. Let's see how ambiguity may occur.

class Addition{
 public int add(int x, int y){
  return x+y;
 }
 public double add(int x, int y){
  return x+y;
 }
}
class Person{
 public static void main(String args[]){
  Addition obj = new Addition();
  System.out.println(obj.add(4,5)); //Ambiguity may occur
}
}


In this example, we have created two methods named as add(), But having a different return type. When we call this method Java compiler show compile time error, because how can Java compiler determine which add() method should be called?


Runtime Polymorphism in Java:

Dynamic polymorphism is also known as runtime polymorphism. Method Overriding is a way to implement runtime polymorphism in Java.

Method Overriding in Java:

Method Overriding is a feature that allows you to redefine the parent class method in the child class based on its requirement.


In other words, whatever methods parent has by default available to the child through inheritance. Sometimes a child may not satisfy with parent methods implementation. Then the child is allowed to redefine that methods based on its requirements, this process is called method overriding.
  • The parent class method which is overridden is known as an overridden method. 
  • The child class method which is overriding is known as an overriding method.
The main purpose of method overriding is that the class give its own implementation to an inherited method without even modifying the parent class code.

Rules for Method Overriding in Java:

  • The method of the parent class and the method of child class must have the same name.
  • The method of the parent class and the method of child class must have the same parameter.
  • Method Overriding in Java is possible through inheritance only.
Therefore to understand the concept of method overriding you should have the basic knowledge of Inheritance in Java


Example of Method Overriding in java:

Let's take a simple example to understand the concept of method overriding.
We have two classes: A parent class Animal and a child class Dog. The Dog class extends the Animal class. Both the class have a common method eat(). The Dog class is giving its own implementation to the eat() method. It is overriding eat() method. 
The child class gives its own implementation so that when it calls this method, it prints Dog is eating instead of Animal is eating.

class Animal{
 public void eat(){
  System.out.println("Animal is eating");
 }
}
class Dog extends Animal{
 public void eat(){
  System.out.println("Dog is eating");
 }
}
class Person{
 public static void main(String args []){
  Dog obj = new Dog();
     obj.eat();
 }
}

Output:
Polymorphism in Java


Overloading vs Overriding in Java:

1. Overloading happens at compile-time whereas Overriding happens at runtime.
2. Overloading is done in the same class whereas for Overriding inheritance are required.
3. In overloading, the parameter must be different while in overriding the parameter must be the same.
4. The main purpose of overloading is to increase the readability of the program while in overriding the class gives its own implementation to an inherited method without even modifying the parent class code.
5. Private, static, and final method can be overloaded but cannot be overridden.
6. Return type can be the same or different in overloading while in overriding return type must be same or covariant return type.
7. Overloading is also known as compile-time polymorphism or static polymorphism or early binding while overriding is also known as runtime polymorphism or dynamic polymorphism or late binding.



You may also like:

Encapsulation in Java OOPs with Example.
Abstract Class in Java.
What are Java Beans?
Java Authentication and Authorization Service - JAAS
JSP Life Cycle.
JDBC Driver in Java.








No comments:

Post a Comment