Types of Polymorphism in Java with Examples
Polymorphism in Java lets you create flexible, reusable code that can work with different classes as if they were from the same family. With polymorphism, you can achieve dynamic behavior and make your code easy to maintain. In this blog post, we will explore the different types of polymorphism in Java, and provide examples to facilitate understanding.
What is Polymorphism in Java?
Polymorphism in Java means that a class can have multiple forms. It lets you treat objects from different classes as if they belong to the same main class. This lets one method or operator handle various types of objects, making your code more flexible and reusable. To learn more about it, consider taking an online Java course.
Different Types of Polymorphism in Java
Polymorphism in Java allows a class to have different forms. This means it can perform various actions based on the context. There are different types of polymorphism in Java that help to achieve these actions. Let’s look at them.
1. Compile-Time Polymorphism
Compile-time polymorphism is a way to handle different situations in a program by having multiple methods with the same name but different parameters in a class. When the program is compiled, the right method is selected based on the method signature, which allows it to work with various data types and arguments effectively.
Example:
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
2. Runtime Polymorphism
Runtime polymorphism is a concept in object-oriented programming where a subclass can change or add its version to a method that was already defined in its parent class. This allows the program to choose the appropriate method to run based on the actual type of the object during runtime.
Example:
class Animal {
void makeSound() {
System.out.println("Generic Animal Sound");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow!");
}
}
Upcasting and Downcasting
Upcasting allows you to treat a subclass object as if it were a superclass object, which lets you use the superclass’s methods on the subclass object. On the other hand, downcasting is when you convert the upcasted object back to its original subclass type.
Example:
Animal = new Cat(); // Upcasting
animal.makeSound(); // Output "Meow!"
Cat cat = (Cat) animal; // Downcasting
cat.makeSound(); // Output "Meow!"
Interface Polymorphism
Interface polymorphism is a concept where a class can implement multiple interfaces, allowing it to show different behaviours depending on those interfaces. This gives the class flexibility and modularity in its design.
Example:
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Bird implements Flyable {
@Override
void fly() {
System.out.println("Bird is flying...");
}
}
class Fish implements Swimmable {
@Override
void swim() {
System.out.println("Fish is swimming...");
}
}
Real-Time Application of Polymorphism in Java
Imagine you’re creating a multimedia player app. This app can handle different types of media like videos, audio files, and images. To make things smooth, you can use polymorphism. It helps you provide a single interface to handle all these different media formats easily. Here’s how to do it:
interface Media {
void play();
}
class Video implements Media {
@Override
void play() {
System.out.println("Playing video...");
// Logic to play video
}
}
class Audio implements Media {
@Override
void play() {
System.out.println("Playing audio...");
// Logic to play audio
}
}
class Image implements Media {
@Override
void play() {
System.out.println("Displaying image...");
// Logic to display the image
}
}
In this code, the ‘Media’ interface has a `play()` method. This method is then implemented by three classes: `Video`, `Audio`, and `Image`. Each of these classes deals with a different type of media and provides its way of executing the `play()` method.
Now, when you create the media player GUI, you can use polymorphism to handle different media types effortlessly.
Media media1 = new Video();
Media media2 = new Audio();
Media media3 = new Image();
// Clicking on the video button will play the video
playButton.addActionListener(e -> media1.play());
// Clicking on the audio button will play the audio
playButton.addActionListener(e -> media2.play());
// Clicking on the image button will display the image
displayButton.addActionListener(e -> media3.play());
Polymorphism helps in separating the media player’s graphical interface code from the particular media formats it supports. This separation allows easy expansion and addition of new media formats without changing the existing code. This method allows cleaner and easier-to-manage code because each media type’s functionality is contained within its class.
Conclusion
In this blog, we have covered different types of polymorphism in Java that provide a powerful way to create flexible and adaptable code. It allows developers to use compile-time and runtime polymorphism, upcasting and downcasting, and interface polymorphism to design robust applications based on OOP principles. Adopting polymorphism in programming promotes the reuse of code, improves how easy it is to maintain, and helps create scalable software systems.