Polymorphism in java

The  Word “Polymorphism” Means “Many Forms”. So Polymorphism Means Many Forms. It Can Be Defined As: When One Thing Has Many Forms. It Is Known As Polymorphism Or Polymorphism Is The Ability Of An Entity To Take Several Forms.

However, Java is an Object-Oriented Programming(OOP) language. Object-Oriented Programming is a guideline for the programmer to program in an efficient and whereas in managed way. Encapsulation, Abstraction, Inheritance, and Polymorphism are the principles of Object-Oriented Programming. though Polymorphism is a combination of two Greek words poly and morphs.

In java, we achieve polymorphism through methods. The two types of polymorphism are known as static polymorphism and dynamic polymorphism  We also see much polymorphic behavior in real-world examples are; Water can change his state at various conditions as solid(ice), liquid(water), and gas(vapor) also. One human being can act or behave differently in a different situation.

Types of polymorphism

1. Static polymorphism:

Static polymorphism is known as compile-time polymorphism. Compile-time polymorphism is handled by the compiler. So, Method overloading is used to achieve Static polymorphism.

Method overloading:

  • When two or more than two methods of a class have the same name but differ from signatures. then it is the method overloading.
  • Where methods are define in the same class or one define in child class and one inherit from a parent class or all inherit from the parent class.
  • The method signature is nothing but the combination of the method name and parameter list. Method signature provides the same method name but takes different parameters.
  • By changing the number of arguments or changing the data type of arguments we can also achieve method overloading.
  • We can not achieve method overloading by changing the return type of methods.
  • Method overloading is also a path to implement polymorphism.

1.  Method overloading is achieve – changing the number of arguments.

In this example, there are two methods in the same class. however, the first add() method performs the addition of two numbers, and the second add() method performs the addition of three numbers.

Output:

2.  Method overloading through changing the datatype of the argument.

In this example, there are two methods in the same class that have a different data type. so, The first add() method takes two integer parameters and the second add method takes two double parameters.

Output:

3.  Method overloading is not possible through changing the return type of methods

In a method, overloading is not done changing the return type of the method only because of similarity.

Output:

2)   Dynamic polymorphism:

Dynamic polymorphism is nothing but run time polymorphism. Run time of the method is handle by JVM. Method overriding is use to achieve dynamic polymorphism.

Method overriding:

  • It is a method in a subclass with the same signature with specific implementation to the subclass.
  • If a subclass provides the specific implementation of the method that has been declared by its parent class then it is also known as method overriding.
  • To achieve method overriding the method also have to follow the same name as in the parent class and the same parameter as in the parent class.
  • In method overriding, there must be a relationship (inheritance) between parent-class (super-class) and child-class (sub-class). 
  • In java private, static, and final methods cannot be overridden as they are also local to the class.
  • though Method overriding is a path to implement polymorphism.  
  • If the Access Modifier of the parent class method is public then during the overriding method the child class method cannot have a private, protected, and default Access modifier.
  • Binding of overridden methods happens at runtime which is – dynamic binding.

1.   Method overriding done by inheritance:

In this example, we inherit the superclass method show() to the subclass method. though it is override to show() method which is define in subclass. 

Output:

2.   Method overriding can not done with different method names in superclass and subclass:

In this example, a subclass is not inherit by the superclass so we can not do method overriding.

Output:

Difference between Method overloading and Method overriding: 
Method overloadingMethod overriding
1.Must have at least two methods with the same name in the same class or also in different inherited classes.Must have at least one method by the same name in both superclass and subclass.
2.Must have a different number of parameters.Must have the same number of parameters.
3.If the number of parameters is the same so then it must have different types of parameters.The types of parameters also must be the same.
4.Method overloading is known as compile-time polymorphism.Method overriding is also known as run time polymorphism.
5.One method does not hide another.The child method hides that of the parent class method.
6.In method overloading, the return type can be the same or different. In method overriding the return type must be the same.
7.Static binding is used for method overloading.Dynamic binding is used for method overriding.

Advantages:

  1. It helps programmers reuse the code and thus saves time.
  2. so, The subclass can give its specific implementation to an inherited method without even modifying the parent class code.

Disadvantages:

  1. Difficult to implement.
  2. It reduces the readability of the program.

Conclusion: 

Polymorphism is one of the Object-Oriented Programming language concepts. so, Which leads to fewer errors in the code and ease of maintenance.

We can also implement it with help of two methods known as Method overloading and Method overriding. In overloading by changing the number or type of arguments and whereas in overriding by hiding the super-class method in subclass method. Hence, It provides flexibility.

Written By: Yash Zadap

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

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