Tuesday, March 28, 2023

Polymorphism in Java

Polymorphism in Java refers to an object's capacity to assume multiple forms, allowing a single action to be performed in a variety of ways. This feature is particularly useful for implementing inheritance in Java. Essentially, polymorphism refers to an entity that can behave differently depending on the situation. For instance, consider a button that can be used to turn a computer on or off. This is an example of polymorphic behavior.

Polymorphism in Java



Polymorphism in Java

Polymorphism in Java refers to an object's capability to take on different forms or exhibit different behaviors. It allows programmers to execute a single action in multiple ways, which makes code more flexible and reusable.

Real-time Example of Polymorphism in Java.

A single person can exhibit different behaviors depending on the context they are in.

Real-time Example of Polymorphism in Java





Types of Polymorphism: 

  1. Compile-time Polymorphism  
  2. Runtime Polymorphism.
Types of Polymorphism




Compile-time Polymorphism in Java 

Compile-time polymorphism in Java, also known as method overloading, refers to the ability of a program to choose the appropriate method or function to execute at compile time, based on the number, type, and order of the arguments passed to it. In other words, method overloading allows multiple methods to have the same name, but with different parameters.

During compilation, the Java compiler determines which method or function to call based on the number and type of arguments passed to it. If two or more methods share the same name but have different parameters, then the compiler will choose the method that best matches the arguments passed.

For example:

public class Calculator
public int add(int num1, int num2)
return num1 + num2; 
 } 
public int add(int num1, int num2, int num3)
return num1 + num2 + num3; 
 } 
public double add(double num1, double num2)
return num1 + num2; 
 } 
public class Main
public static void main(String[] args)
Calculator calculator = new Calculator();
int sum1 = calculator.add(2, 3); 
// Calls the add method with two integers 
int sum2 = calculator.add(2, 3, 4); 
// Calls the add method with three integers

double sum3 = calculator.add(2.5, 3.5);
// Calls the add method with two doubles 
 System.out.println("Sum 1: " + sum1); 
 System.out.println("Sum 2: " + sum2); 
 System.out.println("Sum 3: " + sum3); 
 } 
}

In this example, the Calculator class has three methods with the same name, add, but with different parameters. During compilation, the Java compiler determines which method to call based on the number and type of arguments passed. In the Main class, the add method is called with two integers, three integers, and two doubles, respectively. The compiler determines which method to call for each invocation of the add method.

Compile-time polymorphism is beneficial because it allows a programmer to write cleaner and more efficient code by using the same method name for different behaviors. However, it is limited to choosing the correct method based on the number and type of arguments passed to it during compilation. 

Runtime Polymorphism in Java

Runtime polymorphism in Java, also known as method overriding, refers to the ability of a program to choose the appropriate method to execute at runtime based on the actual object being used. In other words, runtime polymorphism allows a subclass to provide a specific implementation of a method that is already provided by its parent class.

To achieve runtime polymorphism, the subclass must override the method of its parent class. The subclass must provide the same method name, return type, and parameter list as the parent class. However, the implementation of the method in the subclass can be different from the implementation in the parent class.

For example:


class Animal
public void makeSound()
 System.out.println("The animal makes a sound"); 
 } 
class Dog extends Animal
public void makeSound()
 System.out.println("The dog barks"); 
 } 
class Cat extends Animal
public void makeSound()
 System.out.println("The cat meows"); 
 } 
public class Main
public static void main(String[] args)
 Animal animal1 = new Animal(); 
 Animal animal2 = new Dog(); 
 Animal animal3 = new Cat(); 
 animal1.makeSound(); 
 animal2.makeSound(); 
 animal3.makeSound(); 
 } 
}

In this example, the Animal class has a method called makeSound. The Dog and Cat classes extend the Animal class and override the makeSound method with their own implementation. In the Main class, three objects are created, one of type Animal, one of type Dog, and one of type Cat. When the makeSound method is called on each object, the appropriate implementation of the method is executed based on the actual object being used.

Runtime polymorphism is beneficial because it allows a program to choose the appropriate method at runtime based on the actual object being used, which allows for more flexible and dynamic behavior. However, it requires the use of inheritance and can lead to issues if not used carefully, such as accidentally invoking the wrong implementation of a method.

Conclusion 

Polymorphism is an essential concept in Java programming that allows you to write more flexible, reusable, and maintainable code. By using method overloading and method overriding, you can create classes with similar behavior that can be used interchangeably, improving the overall quality of your code.

You Know?

1.What is Polymorphism?

Polymorphism in Java refers to an object's capability to take on different forms or exhibit different behaviors. It allows programmers to execute a single action in multiple ways, which makes code more flexible and reusable. Polymorphism is a useful concept in object-oriented programming, particularly in the implementation of inheritance processes, as it enables child classes to inherit and override methods from parent classes.

2.What is difference between Overloading and Overriding?

Overloading and Overriding are two important concepts in Java that allow for code reuse and improved flexibility in object-oriented programming. Overloading refers to defining multiple methods with the same name but with different parameters, while overriding occurs when a subclass provides its own implementation of a method inherited from its parent class.

3.Which of the following is a type of Polymorphism in Java?

The correct answer is both compile-time and runtime polymorphism are types of polymorphism in Java.

Compile-time polymorphism, also known as method overloading, occurs when multiple methods in a class have the same name but different parameter lists. The compiler determines which method to call based on the number, type, and order of the arguments passed to it.

Runtime polymorphism, also known as method overriding, occurs when a subclass provides a specific implementation of a method that is already provided by its parent class. The method in the subclass must have the same name, return type, and parameter list as the method in the parent class, but the implementation can be different.

Both compile-time and runtime polymorphism are important concepts in object-oriented programming, and they allow for more flexible and dynamic behavior in Java programs.

4.Why is it called Polymorphism?

The term "Polymorphism" comes from two Greek words, "poly" meaning "many" and "morph" meaning "form". In the context of object-oriented programming, polymorphism refers to the ability of an object to take on many forms, or exhibit different behaviors based on its context.

5.Real World Example of Polymorphism in Java.

Polymorphism is a concept that can be observed in many areas of life, including human behavior. A single person can exhibit different behaviors depending on the context they are in. For instance, an individual may act as an employee in their workplace, a customer in a shopping mall, a passenger on a bus or train, a student in a school, and a son or daughter at home. This variety of behaviors exemplifies the idea of polymorphism, which allows for flexibility and adaptability in different situations.

No comments:

Post a Comment