Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) in Java. It allows the same method or object to behave differently depending on the situation, making Java programs more flexible and easier to maintain.
In this article, we will understand what polymorphism means in Java, explore its two main types: compile-time and runtime polymorphism, and look at how method overloading, method overriding, inheritance, and the super keyword are related to polymorphism. We will also cover its advantages and common mistakes to avoid.
Table of Contents
1. What is Polymorphism in Java?
Polymorphism is an OOP (object-oriented programming) concept in Java that means “many forms.” It allows a single method, object, or reference to represent different forms of behavior depending on how it is used.
In Java, polymorphism mainly allows the same method name to perform different tasks. This makes code more flexible, reusable, and easier to maintain.
For example, a sound() method can behave differently for different animals:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
Here, the sound() method has the same name, but its behavior changes depending on the object calling it. The Dog object makes it print “Dog barks,” while the Cat object makes it print “Cat meows.”
Java supports polymorphism mainly through method overloading and method overriding. These are known as compile-time polymorphism and runtime polymorphism, respectively.
2. Types of Polymorphism in Java
-
Runtime Polymorphism: It occurs when Java determines which overridden method to call while the program is running. Method overriding is used to achieve it.
-
Compile-Time Polymorphism: It occurs when Java determines which method to call during compilation. Method overloading is the main way to achieve it.
3. Compile-Time Polymorphism
Compile-time polymorphism is a type of polymorphism where the method to be executed is determined by the compiler during compilation. It is also called static polymorphism or early binding.
In Java, compile-time polymorphism is mainly achieved through method overloading. Method overloading allows a class to have multiple methods with the same name but different parameter lists.
Example of Compile-Time Polymorphism
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20));
System.out.println(calc.add(10, 20, 30));
System.out.println(calc.add(10.5, 20.5));
}
}
Here, the add() method is overloaded with different parameter lists. The compiler determines which version of add() should be called based on the number and type of arguments passed to it.
In short, compile-time polymorphism allows the same method name to perform different operations, with the method call being resolved before the program runs.
4. Runtime Polymorphism
Runtime polymorphism is a type of polymorphism where the method to be executed is determined at runtime, rather than during compilation. It is also known as dynamic polymorphism or late binding.
Runtime polymorphism is achieved through method overriding, where a subclass provides its own implementation of a method that is already defined in its parent class.
Example of Runtime Polymorphism:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.sound();
}
}
Output
Dog barks Cat meows
Here, a1 and a2 are both references of the Animal type, but they refer to different objects. When sound() is called, Java determines at runtime whether to execute the Dog or Cat implementation.
This is why runtime polymorphism is closely associated with inheritance and method overriding.
5. Polymorphism Using Inheritance
Inheritance plays an important role in runtime polymorphism. When a subclass inherits a method from a parent class and overrides it, a parent class reference can be used to refer to an object of the child class.
This allows the same method call to produce different results depending on the actual object.
Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal;
animal = new Dog();
animal.sound();
animal = new Cat();
animal.sound();
}
}
Output:
Dog barks Cat meows
Here, the animal reference is declared as Animal, but it refers to different subclass objects at different times. Java therefore calls the overridden sound() method belonging to the actual object.
This combination of inheritance, method overriding, and parent-class references is the foundation of runtime polymorphism in Java.
6. Polymorphism with super Keyword
The super keyword in Java is used to refer to the immediate parent class. When a subclass overrides a method, super can be used to call the parent class's version of that method.
This is useful when you want to extend or reuse the behavior of the parent method instead of completely replacing it.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
Output:
Animal makes a sound Dog barks
Here, animal is an Animal reference pointing to a Dog object. The overridden sound() method in Dog is called at runtime. Inside that method, super.sound() explicitly calls the parent class's implementation.
Therefore, super allows developers to combine parent and child class behavior while using runtime polymorphism.
7. Advantages of Polymorphism in Java
Polymorphism makes Java programs more flexible, reusable, and easier to maintain. Some of its key advantages include:
- Improves code flexibility: The same method or reference can work with objects of different classes, making the program more adaptable.
- Promotes code reusability: A common parent class or interface can be used to handle multiple child classes without writing separate code for each one.
- Supports loose coupling: Code can depend on a general parent class or interface instead of a specific implementation, making it easier to modify.
- Makes code easier to maintain: New subclasses can often be added without making major changes to existing code.
- Supports dynamic behavior: Runtime polymorphism allows Java to select the appropriate overridden method based on the actual object at runtime.
Overall, polymorphism helps developers write simpler, scalable, and maintainable object-oriented programs.
8. Common Mistakes to Avoid
While using polymorphism in Java, beginners often make mistakes that can lead to compilation errors or unexpected behavior.
- Confusing overloading with overriding: Method overloading is used for compile-time polymorphism, while method overriding is used for runtime polymorphism.
- Changing the method signature while overriding: An overridden method must have the same name and parameter list as the parent method. Changing the parameters creates an overloaded method instead.
- Using weaker access modifiers: An overriding method cannot have more restrictive access than the method in the parent class.
- Forgetting inheritance: Runtime polymorphism through method overriding requires a proper parent-child relationship between the classes.
- Assuming static methods are overridden: Static methods are associated with the class rather than the object, so they are hidden rather than overridden.
- Calling subclass-specific methods through a parent reference: A parent reference can access only members available through the parent type, even when it refers to a child object.
Understanding these common mistakes makes it easier to use polymorphism correctly and avoid unexpected results.
9. Conclusion
Polymorphism allows Java objects and methods to take different forms depending on how they are used. Through method overloading and method overriding, it improves code flexibility, reusability, and maintainability. Understanding compile-time and runtime polymorphism is essential for writing effective object-oriented Java programs.10. FAQs
1. What is polymorphism in Java?
Polymorphism is an OOP concept that allows the same method or reference to behave differently depending on the object or situation.
2. What are the types of polymorphism in Java?
Java mainly supports compile-time polymorphism through method overloading and runtime polymorphism through method overriding.
3. What is compile-time polymorphism?
Compile-time polymorphism occurs when the compiler determines which overloaded method to execute during compilation.
4. What is runtime polymorphism?
Runtime polymorphism occurs when Java determines which overridden method to execute while the program is running.
5. Is method overloading polymorphism?
Yes. Method overloading is an example of compile-time polymorphism in Java.
6. Is method overriding polymorphism?
Yes. Method overriding is the primary way to achieve runtime polymorphism in Java.
0 Comments