Introduction to OOPs
0% completed
Package in Java
Create Package I
Create Package II
Import Class from a Package
Compile a Package
0% completed
Access Modifiers in Java:
Number of Access Modifiers
Access Modifier for main()
Private Data Member
Private Method
0% completed
Classes in Java
Objects in Java
Find the Output
Default Value of an Integer Variable
Create an Object
Superclass
0% completed
Static keyword in java
Locality of Reference
Is Accessible from Static Block?
Locality of Reference II
Static Method
0% completed
Final Keyword in java:
Can Assign?
Can Modify?
Can Override?
Can Inherit?
0% completed
Area of a Rectangle
Complex Number Class
Constructor in Square Class
Fraction Class
Print Name and age
0% completed
Constructor in Java
Constructor in Square Class
Properties of a Constructor
Return Type
Constructor Calling
Constructor Calling II
Is Possible?
0% completed
this Keyword in Java
super keyword in java
Uses of this and super
Can use?
Constructor Chaining
Constructor Chaining II
Instance Initialization block in java
0% completed
Encapsulation in java
Read Only Class
Access Private Members
Concept of Encapsulation
Advantage of Encapsulation
0% completed
Inheritance in java
Car Class
Multilevel Inheritance
Types of Inheritance
Method Overriding
Method Overriding II
Method Overriding III
Polymorphism in Java
0% completed
Polymorphism in java
Shape and Overriding
Types of Polymorphism
Method Overloading
Method Overriding Possible?
Derived Class Reference
0% completed
Abstraction in java
Non-Abstract Method in Abstract Class
Interface contains?
Find the Output
Multiple Inheritance
Implement Multiple Inheritance
Basics Of Java
Report an issue
Polymorphism in java

 

Polymorphism in Java

footer line

Polymorphism

 

Polymorphism is viewed as an essential element of Object-Oriented Programming. Polymorphism is the ability to perform a single action in multiple ways.

Polymorphism is a combination of two Greek words. Poly means "many," and morphs means "forms." So polymorphism signifies a variety of forms. Let's look at a real-world example of polymorphism. 

 

Real-life example:

 

A person may have multiple characteristics at the same time.

A woman, for example, is a sister, a mother, a wife, and an employee all at the same time. As a result, the same person behaves differently in different contexts. This is known as polymorphism.

 

 

Types of Polymorphism in Java

 

In Java, there are two types of polymorphism:

  • Compile-time polymorphism
  • Runtime polymorphism

 

1. Compile-time Polymorphism:

 

Compile-time polymorphism, also known as static polymorphism, is a type of polymorphism that occurs during the compilation process. We can use method overloading to accomplish this form of polymorphism.

 

Method Overloading:

 

When several methods with the same name but different parameters exist in a class, these methods are said to be overloaded. The key benefit of method overloading is that it improves program readability. Methods can be overloaded by using different numbers of arguments or (and) by using different types of arguments. 

 

i) With different numbers of arguments

 

In the example given below, we have written two methods: the first multiply() method, which takes two arguments and multiply the two numbers, and the second multiply() method, which takes three arguments and multiplies the three numbers. Consider the following example:

 

Example:

 

class MethodOverloadingExample {

	// Function with two parameters
	public int multiply(int num1, int num2) {
		return num1 * num2;
	}

	// Function with three parameters
	public int multiply(int num1, int num2, int num3) {
		return num1 * num2 * num3;
	}
}

public class Main {

    public static void main(String args[]) {
		
        MethodOverloadingExample obj = new MethodOverloadingExample();

		// Method calling by passing arguments
		int productOfTwoNumbers = obj.multiply(10, 20);
		int productOfThreeNumbers = obj.multiply(10, 20, 30);

		System.out.println(productOfTwoNumbers);
		System.out.println(productOfThreeNumbers);
	}
}

 

Output:

 

200
6000


 

ii) With different types of arguments

 

We've created two add() methods in the example given below, each with a different data type.

The first add() method requires two integer arguments, while the second add() method requires two double arguments. Consider the following example.
 

Example: 

 

class MethodOverloadingExample {

	// Function with integer parameters
	public int multiply(int num1, int num2) {
		return num1 * num2;
	}

	// Function with double parameters
	public double multiply(double num1, double num2) {
		return num1 * num2;
	}
}

public class Main {

    public static void main(String args[]) {

        MethodOverloadingExample obj = new MethodOverloadingExample();

		// Method calling by passing arguments
		int productInt = obj.multiply(10, 20);
		double productDouble = obj.multiply(10.0, 20.0);

		System.out.println(productInt);
		System.out.println(productDouble);
	}
}

 

Output:

 

200
200.0

 


Runtime Polymorphism 

 

Dynamic polymorphism is another name for runtime polymorphism. In Java, method overriding is a technique for implementing runtime polymorphism. It is also sometimes known as Dynamic method dispatch.

 

Method Overriding

 

Method overriding is a feature that allows you to redefine the base class method in the derived class depending on the requirement of the derived class. In other words, whatever methods are available in the base class are by default also available in the derived class. However, a derived class can be dissatisfied with the base class method implementation at times. Then, the derived class can redefine the method based on its requirements. This process is called method overriding. 

 

Rules for Method Overriding:

  • One class (derived class) should inherit another class (base class).
  • The base class's method and the derived class's method must have the same name.
  • The parameters of the base class's method and the derived class's method must be the same.

 

Example: 

 

class Parent { 
	public void show() 
	{ 
		System.out.println("Inside parent class"); 
	} 
} 

class subclass1 extends Parent { 
	public void show() 
	{ 
		System.out.println("Inside subclass1"); 
	} 
} 

class subclass2 extends Parent { 
	public void show() 
	{ 
		System.out.println("Inside subclass2"); 
	} 
} 


public class Main { 
	public static void main(String args[]) 
	{ 
		Parent p;

		// Upcasting
		p = new subclass1(); 
		p.show(); 
		
		p = new subclass2(); 
		p.show(); 
	} 
}

Output:

 

Inside subclass1
Inside subclass2

 

 

Method Hiding in Java

 

If a subclass defines a static method with the same signature as a static method in a superclass, the subclass method hides the superclass method. It is only possible to hide methods if both the parent and child classes have static methods. 

 

Example:

 

class Parent {
	public static void show() {
		System.out.println("Inside parent class");
	}
}

class Child extends Parent {
	public static void show() {
		System.out.println("Inside subclass");
	}
}

public class Main {
	public static void main(String args[]) {
		Parent p = new Parent();
		p.show();

		Parent c = new Child();
		c.show();
	}
}

 

Output:

 

Inside parent class
Inside parent class

 

In the above example, we assume that p.show() will call the show() method from the parent class and c.show() will call the show() method from the child class, just like in overriding, but since show() is a static method, we have hidden the show() method rather than overriding it.