Polymorphism in Java [Method Overloading & Overriding]

Polymorphism is the ability of an object that helps them take various different forms. To put it simply, it is the process of performing a task in various different methods. In Java, method overloading and method overriding are used to achieve polymorphism.

Polymorphism in Java

1. Method Overloading in Java

In the situation in which a single class has different methods with same name but different parameters, then it is called Method overloading. Method overloading is used in Java to figure out the program quickly as it allows different methods to have the same name, but different signatures.

The signature can be differentiated through the number of input parameters or the type of input parameters. It helps to increase the readability of the program as only one operation that has the same name as the methods have to be performed.

int student(int a, int b) {  
		return a + b;
	}

int student(int a, int b, int c) {    //----> method signature is different
		return a + b + c;
	}

double student(double a, double b, double c) { // --> method signature + data type is different
		return a + b + c;
	}

Advantages of Method Overloading

The main advantage of method overloading is that it makes the program easily readable. There is no need to create and remember different named for functions doing the similar thing by using method overloading.

Different ways to overload the method

The two ways to perform method overloading in Java are:

  1. By changing the number of arguments
  2. By changing the data type

Why is method overloading not possible only by changing the return type of the method?

The reason because of which method overloading is not possible in Java only by changing the return type of the method is ambiguity.

Can Java main method be overloaded?

Yes, method overloading can be used in the main methods to have any number of main methods in a class.

Method overloading and Type promotion

By using method overloading, a single type can be promoted to another type implicitly if there is not a matching data type.

2. Method Overriding in Java

In Java, method overriding is the case in which a subclass/ child class has the same method as of the parent class. This means if a child class provide the same method that the parent class, then it is called method overriding.

It is the property that allows the child class to carry out a specific method that has already been carried out by its parent class. Such overridden method is not seen in the superclass and is not called until the subclass uses the super keyword.

Method overriding is when a method in a child class has a similar name, signature, and return-type as a method in its parent class. It is used in Java to achieve runtime polymorphism.

How Method overriding works? Here a basic example:

First class: Car.java

public class Car {

	void car() {

		System.out.println("I am from Car class");
	}
}

Second class: BMW.java

public class BMW extends Car {

	void run() {
		
		System.out.println("I am from BMW Class");
	}
}

Third class: CarTest.java (main method)

public class CarTest extends Car {

	public static void main(String[] args) {

		BMW obj = new BMW();

		obj.run();
	}
}

Usage of Java Method Overriding

The various usage of method overriding in Java are:

  • Method overriding is used in Java to achieve runtime polymorphism.
  • Method overriding is used to specify methods that will be common to all of its derivatives and that has been already provided by the parent class.
  • Method overriding is used to invoke methods of any subclasses even without the knowledge of the type of the child class object.

Rules for Java Method Overriding

  1. The method in a subclass should not have a different name as in the superclass.
  2. The method in a subclass should have the same parameter as in the superclass.
  3. There must be an IS-A relationship to perform method overriding.
  4. Final methods and private methods cannot be overridden.

Can a static method be overridden?

A static method cannot be overridden which can be proved by runtime polymorphism.

Why cannot static method be overridden?

A static method cannot be overridden because it is bound with class. A static method belongs to the class area not with object area so method overriding cannot be performed on it.

Can Java main method be overridden?

As the main method is also a static method, it cannot be overridden.

Dissimilarities between Method overloading and Method overriding in Java

  1. In method overloading, there is a similar method with different signatures. Whereas in method overriding, there is a similar method with the same signature but different classes.
  2. Method overloading helps to achieve compile-time polymorphism and method overriding helps to achieve runtime polymorphism.
  3. Method overloading increases the readability of the program. On the other hand, method overriding specifies methods that will be common to all of its derivatives and that has been already provided by the parent class.
  4. A method is overloaded within a class, however, a method is overridden in two different classes that have IS-A relationship between them.
  5. The parameter should be different in method overloading but it should be similar in method overriding.

Related posts:

OOPs Concepts in Java – Four Main Concepts

Loops in Java [ For, While & Do While ] with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *