OOP(s): The Polymorphism

OOP(s): The Polymorphism

Same Same but Different.

SpiderMan_Polymorphism.jpg

Scenario

An individual can have different relationships with different people. A woman can be a mother, a daughter, a sister, or a friend, all at the same time, i.e. she performs other behaviors in different situations.

Another one is, We all use calculators on a daily basis. For example, we use a calculator to perform some operations like addition, subtraction, division, and multiplication on two different numbers. When we say numbers what does it mean? Not one right? We can perform on Integers, Float, Long, Imaginary number, etc...

AdorableTastyHerculesbeetle-size_restricted.gif

//Addition on Integer
int addNumbers(int a, int b);

//Addition on Float
float addNumbers(float a, float b);

//Addition on Long
long addNumbers(long a, long b);

//Addition on Double
double addNumbers(double a, double b);

All we do is perform a single operation on different types, in other words, to perform a single task in different ways. And this is polymorphism in java.

The derivation of the word Polymorphism is from two different Greek words- poly and morphs. “Poly” means numerous, and “Morphs” means forms. So, polymorphism means innumerable forms. Polymorphism, therefore, is one of the most significant features of Object-Oriented Programming.

Polymorphism: The Definition

Polymorphism in Java refers to the problem of performing a single operation in several ways.

Languages that do not enable polymorphism are hence referred to as 'Object-Based Languages' rather than 'Object-Oriented Languages.' Ada, for example, is one of these languages. Java is an Object-Oriented Language because it enables polymorphism.

When there is inheritance, polymorphism happens, which means that there are several classes that are connected to each other.

Polymorphism: The Example

"area()" is a method in the superclass "Shapes." Subclasses of "Shapes" include "Triangle," "Circle," "Rectangle," and so on. Each subclass has its own method for calculating area. Subclasses can utilize the "area()" function to determine the area's formula for that shape using Inheritance and Polymorphism.

class Shapes {
  public void area() {
    System.out.println("The formula for area of ");
  }
}
class Triangle extends Shapes {
  public void area() {
    System.out.println("Triangle is ½ * base * height ");
  }
}
class Circle extends Shapes {
  public void area() {
    System.out.println("Circle is 3.14 * radius * radius ");
  }
}
class Main {
  public static void main(String[] args) {
    Shapes myShape = new Shapes();  // Create a Shapes object
    Shapes myTriangle = new Triangle();  // Create a Triangle object
    Shapes myCircle = new Circle();  // Create a Circle object
    myShape.area();
    myTriangle.area();
    myShape.area();
    myCircle.area();
  }
}

Polymorphism: The Types

poly2.jpg

Polymorphism: The Method Overloading

Method Overloading is when a class has multiple methods with the same name, but the number, types, and order of parameters and the return type of the methods are different. Java allows the user freedom to use the same name for various functions as long as it can distinguish between them by the type and number of parameters.

Compile Time Polymorphism

Compile Time Polymorphism In Java is also known as Static Polymorphism. Furthermore, the call to the method is resolved at compile time. Compile-Time polymorphism is achieved through Method Overloading.

mehod_overloading.png

Example

class Shapes {
  public void area() {
    System.out.println("Find area ");
  }
public void area(int r) {
    System.out.println("Circle area = "+3.14*r*r);
  }

public void area(double b, double h) {
    System.out.println("Triangle area="+0.5*b*h);
  }
public void area(int l, int b) {
    System.out.println("Rectangle area="+l*b);
  } 
} 

class Main {
  public static void main(String[] args) {
    Shapes myShape = new Shapes();  // Create a Shapes object

    myShape.area();
    myShape.area(5);
    myShape.area(6.0,1.2);
    myShape.area(6,2);

  }
}

Output: Find area Circle area = 78.5 Triangle area=3.60 Rectangle area=12

Polymorphism: The Method Overriding

Method Overriding is done when a child or a subclass has a method with the same name, parameters, and return type as the parent or the superclass; then that function overrides the function in the superclass. In simpler terms, if the subclass provides its definition to a method already present in the superclass; then that function in the base class is said to be overridden.

Runtime Polymorphism

Runtime polymorphism in Java is also popularly known as Dynamic Binding or Dynamic Method Dispatch. In this process, the call to an overridden method is resolved dynamically at runtime rather than at compile time. You can achieve Runtime polymorphism via Method Overriding.

overidding.png

Example

Let us assume we have a method called run(), in Vehicle class.

Vehicle.java

class Vehicle{  
  void run(){
      System.out.println("Vehicle is moving");      
  }  
}

Now we create class Car and extend Car with Vehicle class. Thus we can override the run() from Car.

Car.java

class Car extends Vehicle{  
  @Override
  void run(){
    System.out.println("car is running safely");
}    
  public static void main(String args[]){  
  Car obj = new Car();
  obj.run();
  }  
}

Output: car is running safely

Advantages of Polymorphism

  • It provides reusability to the code. The classes that are written, tested and implemented can be reused multiple times. Furthermore, it saves a lot of time for the coder. Also, the one can change the code without affecting the original code.
  • A single variable can be used to store multiple data values. The value of a variable you inherit from the superclass into the subclass can be changed without changing that variable’s value in the superclass; or any other subclasses.
  • With lesser lines of code, it becomes easier for the programmer to debug the code.

Method Overloading Vs. Method Overriding

Screenshot from 2022-08-06 22-35-00.png

More on GFG

Finally

Difference-between-Method-Overloading-and-Method-Overriding-in-Java-300x166.gif