Polymorphism in Java

Polymorphism is a key concept in object-oriented programming that allows objects of different classes to be treated as if they were objects of the same class. In Java, polymorphism can be achieved through inheritance, interfaces, and method overloading.


  1. 1. Inheritance Polymorphism:Inheritance polymorphism is achieved when a subclass inherits from a superclass and can be treated as an instance of the superclass. This means that an object of the subclass can be assigned to a reference variable of the superclass type. For example:

  2. class Animal {
        public void makeSound() {
            System.out.println("Some sound");
        }
    }
    
    class Cat extends Animal {
        @Override
        public void makeSound() {
            System.out.println("Meow");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal myCat = new Cat();
            myCat.makeSound(); // Outputs "Meow"
        }
    }
    


    In this example, the Cat class inherits from the Animal class and overrides the makeSound() method to output "Meow". An instance of the Cat class is created and assigned to a variable of the Animal type. When the makeSound() method is called on the variable, the output is "Meow", which is the behavior of the Cat class.


  1. 2. Interface Polymorphism:Interface polymorphism is achieved when multiple classes implement the same interface, and can be treated as instances of the interface. This means that an object of any class that implements the interface can be assigned to a reference variable of the interface type. For example:

  2. interface Animal {
        void makeSound();
    }
    
    class Cat implements Animal {
        @Override
        public void makeSound() {
            System.out.println("Meow");
        }
    }
    
    class Dog implements Animal {
        @Override
        public void makeSound() {
            System.out.println("Woof");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal myCat = new Cat();
            Animal myDog = new Dog();
            myCat.makeSound(); // Outputs "Meow"
            myDog.makeSound(); // Outputs "Woof"
        }
    }
    


    In this example, the Cat and Dog classes implement the Animal interface, which defines the makeSound() method. An instance of each class is created and assigned to a variable of the Animal type. When the makeSound() method is called on the variables, the appropriate sound is output, which is the behavior of each class.


  1. 3. Method Overloading Polymorphism:Method overloading polymorphism is achieved when multiple methods have the same name but different parameters, and can be called with the same name but different arguments. This means that the appropriate method is called based on the number and types of the arguments provided. For example:

  2. class Calculator {
        public int add(int a, int b) {
            return a + b;
        }
    
        public double add(double a, double b) {
            return a + b;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Calculator myCalc = new Calculator();
            System.out.println(myCalc.add(2, 3)); // Outputs 5
            System.out.println(myCalc.add(2.5, 3.7)); // Outputs 6.2
        }
    }
    


    In this example, the Calculator class has two add() methods with the same name but different parameters. An instance of the Calculator class is created and used to call each method with different arguments. The appropriate method is called based on the number and types of the arguments provided, which is the behavior of each method.


    In summary, polymorphism in Java allows objects of different classes to be treated as if they were objects of the same class, providing flexibility and extensibility in object-oriented programming. Inheritance polymorphism allows a subclass to inherit from a superclass and be treated as an instance of the superclass. Interface polymorphism allows multiple classes to implement the same interface and be treated as instances of the interface.


    Method overloading polymorphism allows multiple methods to have the same name but different parameters and be called with the same name but different arguments, selecting the appropriate method based on the number and types of the arguments provided.