Monday, 1 May 2023

What is Polymorphism in Java? with example.


Polymorphism Is A Fundamental Concept In Object-oriented Programming (OOPS) That Allows An Object To Take On Multiple Forms. In Java, Polymorphism Can Be Achieved Through Two Mechanisms: Method Overloading And Method Overriding. Here's An Example Of Each:


1. Method Overloading: Method Overloading Is When Two Or More Methods In A Class Have The Same Name But Different Parameters. Java Can Determine Which Method To Call Based On The Number And Types Of The Arguments Passed To The Method. Here's An Example:


```

public class MathOperations {

   public int add(int x, int y) {

      return x + y;

   }

   public int add(int x, int y, int z) {

      return x + y + z;

   }

   public double add(double x, double y) {

      return x + y;

   }

}


// Here's how you can use the MathOperations class:


MathOperations math = new MathOperations();

System.out.println(math.add(1, 2)); // prints 3

System.out.println(math.add(1, 2, 3)); // prints 6

System.out.println(math.add(1.5, 2.5)); // prints 4.0

```


In this example, the `MathOperations` class has three `add` methods with the same name but different parameters. Java can determine which method to call based on the arguments passed to it. In the example, the first `add` method is called with two integer arguments, the second `add` method is called with three integer arguments, and the third `add` method is called with two double arguments.


2. Method Overriding: Method overriding is when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. Here's an example:


```

public class Animal {

   public void makeSound() {

      System.out.println("Animal makes a sound");

   }

}


public class Cat extends Animal {

   public void makeSound() {

      System.out.println("Meow");

   }

}


// Here's how you can use the Cat class:


Animal animal = new Animal();

animal.makeSound(); // prints "Animal makes a sound"


Cat cat = new Cat();

cat.makeSound(); // prints "Meow"

```


In This Example, The `Animal` Class Has A `Makesound` Method That Prints A Generic Message. The `Cat` Class Extends The `Animal` Class And Overrides The `Makesound` Method To Print "Meow". When An `Animal` Object Is Created And The `Makesound` Method Is Called, The Generic Message Is Printed. When A `Cat` Object Is Created And The `Makesound` Method Is Called, "Meow" Is Printed Instead.

Polymorphism Is A Powerful Concept In Java That Allows For Flexible And Extensible Code. By Using Method Overloading And Method Overriding, You Can Write Code That Is Easier To Read, Understand, And Maintain.

No comments:

Post a Comment

What is Java Permutation and Combination Program?

Here's A Java Program That Calculates Permutations And Combinations: ```Java Import Java.Util.Scanner; Public Class Permutationcombinati...