Back to: Java Tutorials
Polymorphism is a fundamental concept of object-oriented programming (OOP) in Java. It refers to the ability of an object to take on many forms, and it enables objects to behave differently depending on the context in which they are used. In this tutorial, we will discuss what polymorphism is in Java and how to implement it with examples.
Polymorphism in Java
Polymorphism is the ability of an object to take on many forms. It allows objects to behave differently depending on the context in which they are used. There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism.
Compile-Time Polymorphism
Compile-time polymorphism is also known as method overloading. It allows us to define multiple methods with the same name but different parameters in a class. The compiler chooses the appropriate method to call based on the number and type of arguments passed to the method.
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.add(1, 2));
System.out.println(MathUtils.add(1.0, 2.0));
System.out.println(MathUtils.add(1, 2, 3));
}
}
In this example, we have defined three methods with the same name “add” in the “MathUtils” class. The first method takes two integer parameters, the second method takes two double parameters, and the third method takes three integer parameters. In the “Main” class, we call each of these methods with different arguments. The compiler chooses the appropriate method to call based on the number and type of arguments passed to the method.
Runtime Polymorphism
Runtime polymorphism is also known as method overriding. It allows a subclass to provide its own implementation of a method that is already defined in its superclass. When we call the method on an object of the subclass, it calls the overridden method in the subclass instead of the inherited method in the superclass.
public class Animal {
public void makeSound() {
System.out.println("Animal is making a sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog is barking");
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat is meowing");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.makeSound();
animal2.makeSound();
}
}
In this example, we have defined three classes – “Animal”, “Dog”, and “Cat”. The “Animal” class has a method called “makeSound”, while the “Dog” and “Cat” classes override this method to provide their own implementation. In the “Main” class, we create objects of the “Dog” and “Cat” classes and assign them to variables of the “Animal” type. We then call the “makeSound” method on each of these variables. The method called depends on the actual type of the object, not the reference type.
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author