Polymorphism in java

 

Polymorphism

 

-Introduction to java programming

  • Polymorphism in Java refers to an object's capacity to take several forms. Simply put, polymorphism allows us to accomplish the same action in multiple ways in Java. Any Java object that passes more than one IS-A test is considered polymorphic, and all Java objects are polymorphic because they have passed the IS-A test for their own type as well as the class Object. Polymorphism in Java is divided into two types: compile-time polymorphism and runtime polymorphism.

What is polymorphism and how does it affect you?

  • Polymorphism is derived from two Greek words: polymorphism and morphism. "Poly" stands for "many," and "morphs" stands for "forms." As a result, polymorphism denotes the existence of numerous variations. One of the most important characteristics of Object-Oriented Programming is polymorphism.

In Java, what is polymorphism?

  • In Java, polymorphism is described as a job that can perform a single operation in multiple ways.
  • Object-Oriented Languages can't be called that if they don't support polymorphism. Object Base Languages, on the other hand, are what they're called. One example of such a language is Ada.
  • Java is an Object-Oriented Language because it enables polymorphism. When there is inheritance, or when there are numerous classes that are connected to each other, polymorphism develops.
  • In Java, inheritance is a strong feature. Inheritance allows one class to have access to another's properties and characteristics. In Java, polymorphism allows us to use these inherited attributes for a variety of purposes. As a result, we can perform the same activity in a variety of ways.

Polymorphism in the Real World

  • A person might have a variety of relationships with different persons. A woman can be a mother, a daughter, a sister, and a friend all at once, i.e. she can act in different ways in different contexts.
  • The human body is made up of various organs. The heart is in charge of blood flow, the lungs are in charge of breathing, the brain is in charge of cognitive activity, and the kidneys are in charge of excretion. As a result, we have a standard method function that behaves differently based on the body organ.

Example of Polymorphism in Java

  • The function "area()" is part of the "Shapes" superclass. "Triangle," "circle," "Rectangle," and other subclasses of "Shapes" are examples. Each subclass has a different method for computing area. Subclasses can utilize the "area()" method 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();

  }

}

Output:

The formula for the area of Triangle is ½ * base * height
The formula for the area of the Circle is 3.14 * radius * radius


Types of Polymorphism

In Java, there are two ways to implement polymorphism:

1) Overloading and 2)Overriding of Methods

In Java, what is method overloading?

  • Method overloading is a procedure that allows you to have numerous methods with the same name in the same class, all of which behave differently. When a class has multiple methods with the same name, this is known as method overloading.

In Java, an example of method overloading

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

In Java, what is method overriding?

Method overriding is a procedure in which a subclass or child class declares the same method as the parent class.

In Java, an example of method overriding

class Vehicle{ 

  //defining a method 

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

//Creating a child class 

class Car2 extends Vehicle{ 

  //defining the same method as in the parent class 

  void run(){System.out.println("car is running safely");} 

  

  public static void main(String args[]){ 

  Car2 obj = new Car2();//creating object 

  obj.run();//calling method 

  } 

}  

Output:

Car is running safely

Polymorphism in Java is also divided into two types:

1)Static / Compile-Time Polymorphism is a type of polymorphism that occurs during the compilation process.

2)Runtime Polymorphism / Dynamic Polymorphism

In Java, what is Compile-Time Polymorphism?

Static polymorphism is another name for compile-time polymorphism in Java. The call to the method is resolved at build time in this process. Method Overloading is used to achieve compile-time polymorphism. Operator Overloading can also be used to achieve this form of polymorphism. Java, on the other hand, does not support Operator Overloading.

When a class contains numerous methods with the same name, but the number, types, and order of parameters, as well as the return type of the methods, is different, this is known as method overloading. Java permits the user to use the same name for many functions as long as the type and number of parameters can be distinguished

Example of Compile- Time Polymorphism in java

We will do addition in java and understand the concept of compile-time polymorphism using subtract() 

package staticPolymorphism;

public class Addition

{

void sum(int a, int b)

{

int c = a+b;

System.out.println(“ Addition of two numbers :” +c); }

void sum(int a, int b, int e)

{

int c = a+b+e;

System.out.println(“ Addition of three numbers :” +c); }

public static void main(String[] args)

{

Addition obj = new Addition();

obj.sum ( 30,90);

obj.sum(45, 80, 22);

}

}

 

The output of the program will be: 

Sum of two numbers: 120 

Sum of three numbers: 147 

In this program, the sum() method was overloaded with two types with different parameters. 

This is the basic concept of compile-time polymorphism in java where we can perform various operations by using multiple methods having the same name.

Conclusion: -

  • Java's strategy has always been to take the greatest elements of object-oriented programming and combine them into a single language. As a result, Java's operator overloading feature was removed since it caused further confusion.
  • Polymorphism is a feature of Java's object-oriented programming language that allows a single task to be completed in multiple ways. In the technical realm, polymorphism in Java allows you to define one interface and have many implementations.

Comments