Java Polymorphism
Understanding Java Polymorphism
In this tutorial, we will explore one of the fundamental concepts in Java and Object-Oriented Programming (OOP) called Polymorphism. This concept is often tricky for beginners, but we'll break it down into bite-sized, understandable pieces.
What is Polymorphism?
The term Polymorphism is derived from two Greek words: Poly (many) and morphs (forms). In the context of OOP, Polymorphism is the ability of an object to take on many forms. It allows us to perform a single action in different ways.
So, in Java, polymorphism allows us to define one interface or method and have multiple implementations. This can make our programs more modular and scalable, and allow for more complex behaviors with less code.
Types of Polymorphism
There are two types of polymorphism in Java:
- Compile-time Polymorphism: Also known as static or early binding. This type of polymorphism is achieved by function overloading or operator overloading.
- Runtime Polymorphism: Also known as dynamic or late binding. This type of polymorphism is achieved by method overriding.
Compile-time Polymorphism
Compile-time polymorphism or static polymorphism is a type of polymorphism that resolves during compile time. In Java, we can achieve compile-time polymorphism through method overloading.
Method Overloading
Method overloading is a feature in Java that allows a class to have two or more methods having the same name but different in parameters. This is an example of compile-time polymorphism because the method to be invoked is determined during the compile time.
class Adder {
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }
}
public class TestOverloading {
public static void main(String[] args) {
System.out.println(Adder.add(10, 20));
System.out.println(Adder.add(10, 20, 30));
}
}
Runtime Polymorphism
Runtime polymorphism or dynamic polymorphism is a process in which a function call to the overridden method is resolved during runtime. This is also called as late binding. In Java, we can achieve runtime polymorphism through method overriding.
Method Overriding
Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already provided by its parent class. The method in the subclass must have the same name, return type, and parameters as the one in its parent class.
class Animal {
void eat() { System.out.println("eating..."); }
}
class Dog extends Animal {
void eat() { System.out.println("eating bread..."); }
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
a.eat();
}
}
Conclusion
Polymorphism is a powerful concept in Java and OOP. It allows us to create more flexible and dynamic code, and is a key part of many design patterns in Java. Understanding polymorphism and how to use it effectively can greatly improve your Java programming skills.
In this tutorial, we've covered the basics of polymorphism, including compile-time polymorphism (with method overloading) and runtime polymorphism (with method overriding). Practice these concepts and try to use them in your own code to get comfortable with polymorphism in Java.