Last chance! 50% off DataCamp Premium
Sale ends in
Polymorphism in Java is a core concept of object-oriented programming (OOP) that allows objects to be treated as instances of their parent class. It facilitates flexibility and the ability to define methods in multiple forms. Polymorphism is primarily achieved through method overriding and method overloading.
Polymorphism is used to perform a single action in different ways. It allows for the implementation of dynamic method dispatch, where the method to be executed is determined at runtime. This is beneficial for implementing a cleaner and more maintainable codebase.
public class OverloadingExample {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
System.out.println("Sum of integers: " + obj.add(5, 10));
System.out.println("Sum of doubles: " + obj.add(5.5, 10.5));
}
}
In this example, the add method is overloaded with two different parameter lists. The appropriate method is called based on the arguments passed.
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class OverridingExample {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Outputs "Dog barks"
}
}
Here, the Dog class overrides the sound method of the Animal class. The sound method of the Dog class is invoked at runtime.
@Override annotation to indicate that a method is intended to override a method in a superclass, which helps prevent errors.