πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Polymorphism in Java  


Introduction
  • Polymorphism is one of the main concepts of Object-Oriented Programming (OOP) in Java.
  • Poly β†’ many, Morph β†’ forms; so Polymorphism means "many forms".
  • It means the ability of a single entity (method, object, or operator) to behave in multiple ways.
  • Java uses polymorphism to let us write flexible and reusable code.
  • Real-world Examples :-
    • A person: acts as a teacher, father, son β€” different roles.
    • Water: takes shape of the container it’s in.
    • Sound: same sound word used in different tones.
  • Advantage of Polymorphism :-
    • Increases flexibility and reusability.
    • Allows code extensibility without modifying existing code.
    • Supports single task, multiple implementations.
    • Enhances maintainability and scalability.
Types of Polymorphism in Java :-
  1. Compile-Time Polymorphism
    • It is also known as Static Binding or Early Binding.
    • It is achieved by Method Overloading or Operator Overloading.
    • In compile-time polymorphism, the Java compiler decides at compile time which overloaded method or operator to invoke based on the method signature and reference type.
    • Program (Method Overloading):
      class Calculator
      {
          void add(int a, int b)
          {
              System.out.println(a+b);
          }
      
          void add(double a, double b)
          {
              System.out.println(a+b);
          }
      }
    • Here:
      • Calculator calc = new Calculator(); // Reference type = Calculator
      • calc.add(5, 10); // method signature = add(int, int) β†’ decided at compile-time
    • Click here to read more about Method Overloading
  2. Runtime Polymorphism
    • It is also known as Dynamic Binding or Late Binding.
    • It is achieved by Method Overriding.
    • In runtime polymorphism, the JVM (Java Virtual Machine) decides at runtime which overridden method to invoke based on the actual object (not the reference type).
    • Program (Method Overriding):
      class Animal
      {
          void makeSound()
          {
              System.out.println("Some generic sound");
          }
      }
      
      class Dog extends Animal
      {
          void makeSound()
          {
              System.out.println("Dog barks");
          }
      }
    • Here:
      • Animal obj = new Dog(); // Reference type = Animal, Object type = Dog
      • obj.makeSound(); // Method decided at runtime β†’ calls Dog's makeSound()
    • Click here to read more about Method Overriding

Important Points
  • Some important points to remember about polymorphism in Java:
    1. Polymorphism is mainly achieved through method overloading and method overriding.
      • Overloading β†’ Compile-time polymorphism
      • Overriding β†’ Runtime polymorphism
    2. Polymorphism supports the Open/Closed Principle.
      • Code is open for extension but closed for modification.
    3. Polymorphism helps reduce code duplication by reusing the same interface or method name across different types.
    4. Upcasting enables runtime polymorphism.
      • Parent class reference can hold a child class object.
      • Example:
        Animal myDog = new Dog();