Polymorphism vs Overloading vs Overriding
Someone asked me What are the difference between Polymorphism and Overriding in Java and the similar difference between Polymorphism and Overloading. Well, they are not two different things, Polymorphism is an object oriented or OOPS concept like Abstraction, Encapsulation or Inheritance which facilitate the use of the interface and allows Java program to take advantage of dynamic binding in Java. Polymorphism is also a way through which a Type can behave differently than expected based upon which kind of Object it is pointing. Overloading and overriding are two forms of Polymorphism available in Java.
Both overloading and the overriding concept are applied on methods in Java. Since polymorphism literally means taking multiple forms, So even though you have the name of the method same in the case of overloading and overriding, an actual method called can be any of those multiple methods with the same name. Let's see some more details on method overloading and overriding to understand how polymorphism relates to overloading and overriding and How they are different.
Both overloading and the overriding concept are applied on methods in Java. Since polymorphism literally means taking multiple forms, So even though you have the name of the method same in the case of overloading and overriding, an actual method called can be any of those multiple methods with the same name. Let's see some more details on method overloading and overriding to understand how polymorphism relates to overloading and overriding and How they are different.
Polymorphism vs Overriding
Overriding is a form of polymorphism which is used in Java to dynamically bind method from the subclass in response to a method call from sub class object referenced by superclass type. Method overriding is bonded using dynamic binding in Java.
Suppose you have two methods size() in both base class and derived class and Base class variable is pointing to an object which happens to be subclass object at runtime then method from subclass will be called, i.e. overridden method will be called.
This allows to program for interface than implementation, a popular OOPS design principle because Polymorphism guarantees to invoke correct method based upon the object. Method overriding is key for much flexible design pattern in Java.
See What is method overriding in Java and Rules of method Overriding for examples and more details.
Suppose you have two methods size() in both base class and derived class and Base class variable is pointing to an object which happens to be subclass object at runtime then method from subclass will be called, i.e. overridden method will be called.
This allows to program for interface than implementation, a popular OOPS design principle because Polymorphism guarantees to invoke correct method based upon the object. Method overriding is key for much flexible design pattern in Java.
See What is method overriding in Java and Rules of method Overriding for examples and more details.
Polymorphism vs Overloading
Method overloading is another form of Polymorphism though some people argue against that. In the case of overloading, you also got multiple methods with the same name but different method signature but a call to correct method is resolved at compile time using static binding in Java. Overloading is a compile time activity oppose to Overriding which is runtime activity. Because of this reason overloading is faster than method overriding in Java. Though beware with an overloaded method which creates conflict e.g. methods with only one parameter e.g. int and long etc. See What are method overloading in Java for example and complete details.
![]() |
An Example of Polymorphism in Java |
An example of Polymorphism in Java

import java.util.ArrayList;
import java.util.List;
abstract class Pet{
public abstract void makeSound();
}
class Cat extends Pet{
@Override
public void makeSound() {
System.out.println("Meow");
}
}
class Dog extends Pet{
@Override
public void makeSound() {
System.out.println("Woof");
}
}
import java.util.List;
abstract class Pet{
public abstract void makeSound();
}
class Cat extends Pet{
@Override
public void makeSound() {
System.out.println("Meow");
}
}
class Dog extends Pet{
@Override
public void makeSound() {
System.out.println("Woof");
}
}
Let's test How Polymorphism concept work in Java:
/**
*
* Java program to demonstrate What is Polymorphism
* @author Javin Paul
*/
public class PolymorphismDemo{
public static void main(String args[]) {
//Now Pet will show How Polymorphism work in Java
List<Pet> pets = new ArrayList<Pet>();
pets.add(new Cat());
pets.add(new Dog());
//pet variable which is type of Pet behave different based
//upon whether pet is Cat or Dog
for(Pet pet : pets){
pet.makeSound();
}
}
}
Output:
Meow
Woof
*
* Java program to demonstrate What is Polymorphism
* @author Javin Paul
*/
public class PolymorphismDemo{
public static void main(String args[]) {
//Now Pet will show How Polymorphism work in Java
List<Pet> pets = new ArrayList<Pet>();
pets.add(new Cat());
pets.add(new Dog());
//pet variable which is type of Pet behave different based
//upon whether pet is Cat or Dog
for(Pet pet : pets){
pet.makeSound();
}
}
}
Output:
Meow
Woof
In Summary, you can not compare Polymorphism with method overloading or override. Polymorphism is the ability of a variable to behave differently based upon which kind of Object it is referring. They are Java programming language's way to implement polymorphism in language.
Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]
Other Java programming and OOP tutorials You may like
- My favorite courses to learn object-oriented programming
- 5 difference between Hashtable and HashMap in Java
- 5 Free Object-Oriented programming course for beginners
- What is a factory method design pattern in Java
- Top 5 Courses to Learn Design Patterns in Java
- What is Factory method design pattern in Java
- What is Observer design pattern in Java
- How to implement the Singleton pattern in Java
- How to implement the Producer-Consumer pattern in Java
- 5 Free Data Structure and Algorithms Courses
- Difference between HashMap and ConcurrentHashMap in Java
- 7 best OOP design pattern courses for Java developers
- 10 Java Coding Interview Questions and Answers for Java beginners.
- 5 Free Spring Framework Courses for Java Developers
- Difference between TreeSet and HashSet in Java
- 10 Free Courses to learn Java for Beginners and Experienced Programmers
Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
Seems you are a C# programmer.
ReplyDeleteCalm down! You've failed to criticize the article, so I will assume that you agree with it, languages aside. And yes, I am replying to a comment made 4+ years later; deal with it!
Delete