Polymorphism in Java OOPs (With Examples) - Types of Polymorphism

 

Polymorphism - Types of Polymorphism


In this article, we will learn about the different types of polymorphism in Java with examples. Polymorphism is an object-oriented programming concept that allows an entity to take on multiple forms. The term comes from the Greek words "poly" (many) and "morphos" (forms).

Polymorphism

In Object-oriented programming, polymorphism is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

There are two main types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. Compile-time polymorphism is also known as static binding, while runtime polymorphism is also known as dynamic binding.

Static binding occurs when the compiler can know, at compile time, which method will be invoked on an object, based on the type of the object. An example of static binding in Java would be if you have a class hierarchy with a superclass and subclass, and you invoke a method on an instance of the superclass. The compiler knows which method to invoke based on the type of the object (i.e. the superclass), so it can bind the method call to the correct method definition at compile time.

Dynamic binding occurs when the compiler cannot know, at compile time, which method will be invoked on an object. This happens when you have a reference to an object whose type is not known at compile time (e

Syntax of Polymorphism

Polymorphism is a technique in Java programming that allows an object to take on multiple forms. The most common use of polymorphism in Java is with method overriding, where a subclass can override the methods of its parent class. However, there are other uses for polymorphism in Java, as well.

In this blog post, we'll take a closer look at the different types of polymorphism in Java, as well as some examples of how each type can be used. By the end of this post, you should have a good understanding of what polymorphism is and how it works in Java. Let's get started!

Polymorphism Code Example

In this blog post, we'll be looking at an example of polymorphism in Java. We'll start with a brief introduction to the concept of polymorphism, before moving on to look at the different types of polymorphism that exist. Finally, we'll see how all of this can be used in practical code examples.

Shape Class

public class Shape 

{

   public void shape()

  {

 

  }

}

Circle Class

public class Circle extends Shape
{
private double Pi;
private double radius;
public Circle(double pi, double radius) 
{
super();
Pi = pi;
this.radius = radius;
}
public double getPi() {
return Pi;
}
public void setPi(double pi) {
Pi = pi;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public void shape() {
super.shape();
double area = 0.0;
area = Pi*(radius*radius);
System.out.println("Area of circle: "+area);
}

}

Rectangle Class

public class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public void shape() {
super.shape();
double area =0.0;
area=length*width;
System.out.println("Area of Rectangle: "+area);
}
}

Triangle Class

public class Triangle extends Shape
{
 private double length;
 private double width;
public Triangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public void shape() {
super.shape();
double area =0.0;
area=0.5*(length*width);
System.out.println("Area of Triangle: "+area);
}
 
}

Square Class

public class Square extends Shape
{
private double length;
public Square(double length) {
super();
this.length = length;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public void shape() {
super.shape();
double area =0.0;
area=2*length;
System.out.println("Area of Rectangle: "+area);
}
}

Test Class

import java.util.Scanner;

public class Test {

public static void main(String[] args) 
{
Shape s = new Shape();
Shape c = new Circle(3.14,2.3);
Shape t = new Triangle(2,3);
Shape r = new Rectangle(14,6.7);
Shape sq = new Square(2.4);
   
do{ System.out.println("***********************");
System.out.println("Press 1 for Circle area\nPress 2 for Triangle Area\nPress 3 for Rectangle area\n"
+ "Press 4 for Square Area\nPress 5 to Exit");
System.out.println("***********************");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice)
{
case 1:
{
c.shape();
break;
}
case 2:
{
t.shape();
break;
}
case 3:
{
r.shape();
break;
}
case 4:
{
sq.shape();
break;
}
case 5:
{
System.exit(0);
}
}
}while(true);

}

}

Types of Polymorphism With Examples

In Java, there are two types of polymorphism: 

  • Compile-time polymorphism
  • Runtime polymorphism

In this article, we'll take a closer look at both types of polymorphism and provide some examples to help illustrate how they work.

Compile-time polymorphism

Compile-time polymorphism is also known as static binding or early binding. This type of polymorphism occurs when the compiler can determine which method to call at compile time based on the static type of the object. An example of this would be a method that is overloaded, meaning that there are multiple methods with the same name but different signatures. The signature of a method includes the number and types of arguments that the method takes. When an overloaded method is called, the compiler selects the correct method to call based on the number and types of arguments that are being passed to the method.

Runtime polymorphism

Runtime polymorphism, on the other hand, is also known as dynamic binding or late binding. This type of polymorphism occurs when the compiler cannot determine which method to call at compile time and instead relies on dynamic (runtime) information to make this determination. An example of this would be a situation where you have a superclass and subclass with methods that have the same name but different implementations. When

Need (Use) of Polymorphism

In Object Oriented Programming, polymorphism is the ability of an object to take on many different forms. The word "poly" means "many" and "morph" means "form". So, in simple terms, polymorphism is the ability of an object to take on many different forms.

Polymorphism is a very important concept in OOPs. It is used to design programs that are easy to extend and maintain. Polymorphism also makes it possible to create relationships between objects that are not necessarily of the same type.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism.

Compile-time polymorphism is also known as static binding or early binding. In this type of polymorphism, the type of an object is determined at compile time. For example, method overloading is a form of compile-time polymorphism.

Runtime polymorphism is also known as dynamic binding or late binding. In this type of polymorphism, the type of an object is determined at runtime. For example, method overriding is a form of runtime polymorphism.

Code School

Code School is sharing assignments, books, labs, lectures, notes, OOP and java code, projects, and semester projects.

Post a Comment

Previous Post Next Post