Method Overriding in Java – Tutorial and Example

In this article we will discuss, what is method overriding in Java.

What is a Method Overriding in Java?

When a Derived class overrides a method of its Base class then its called Method overriding.

Why do we need of method overriding in Java?

Every method in a class represents some behavior. For example, We have a class Encryptor which has a behavior/method encrypt(), that basically encrypts the passed string by reversing its content i.e.

class Encryptor
{
	public String encrypt(String msg)
	{
		StringBuilder builder = new StringBuilder(msg);
		builder.reverse();
		return builder.toString();
	}
}

In our code we are using some framework that uses the Encryptor class object to encrypt the messages. But a new requirement comes i.e.  For some new scenarios we need to use different encryption logic like, instead of reversing contents, increment each character by 2.

Now, to handle this new requirement we need to create an another class for encryption, but framework we are using just knows the Encryptor class. Therefore, we need to extend this new class from Encryptor class. It overrides the encryption behavior of its base class i.e. overrides the encrypt() method. Let’s see how to do that,

Overriding a method in Derived class

To override a base class method in derived class, we need to follow certain rules. First basic rule is this overridden method in derived class should be of same signature, as of Base class’s method. Lets see an example by creating a Derived class of above mentioned Encryptor class, which will override the encrypt() method to fulfill the requirement i.e.

class DerivedEncryptor extends Encryptor
{
	public String encrypt(String msg)
	{
		StringBuilder builder = new StringBuilder(msg);
		for (int i = 0; i < builder.length(); i++)
			builder.setCharAt(i, (char)(builder.charAt(i) + 2));

		return builder.toString();
	}
}

Here, we have overridden the encrypt() method of Base class.

Now lets see how to use function overriding. We will create an object of DerivedEncryptor class and assign it to base class (Encryptor) reference i.e.

Encryptor encrytor2 = new DerivedEncryptor();
String msg = encrytor2.encrypt("Hello");
System.out.println(msg);

Now when we call the encrypt() function from this reference, then it will call the DerivedEncryptor class’s encrypt() function. It’s because the actual object pointed by this reference is of theDerivedEncryptor class.

This is also called Runtime binding or Dynamic Polymorphism in Java i.e. JVM will decide at run-time which function should be called based on the actual object pointed by the reference. For example, in this case , base class reference was pointing to the Derived class object, hence due to run-time binding, Derived class’s encrypt() function is called.

Rules of Method Overriding in Java

There are certain rules which we need to follow, while overriding a method in Derive class. We will discuss each of them in detail but lets list them here i.e.

  1. Overriding Method in derived class should have same signature. Although in some case return type can change i.e. overridden method in derived class can return object of sub-type of what Base class’s method is returning. We will discuss this in detail in next article.
  2. In derived class, overriding method should be of same or greater accessibility i.e. public method in Base class cannot be protected in Derived class.  Whereas, protected method in Base class can be public in derived class. We will discuss this in detail in next article.
  3. Overriding Method in derived class can not throw checked exceptions which are higher in heirarcy than the  checked exception thrown by the Base class’s method. We will discuss this in detail in next article.

Complete Example is as follows,

package com.thispointer.method.overriding;

class Encryptor {
	public String encrypt(String msg) {
		StringBuilder builder = new StringBuilder(msg);
		builder.reverse();
		return builder.toString();
	}
}

/*
 * This class will override the ecrypt() method of base class
 */
class DerivedEncryptor extends Encryptor {
	@Override
	public String encrypt(String msg) {
		StringBuilder builder = new StringBuilder(msg);
		for (int i = 0; i < builder.length(); i++)
			builder.setCharAt(i, (char) (builder.charAt(i) + 2));

		return builder.toString();
	}
}

public class Example {

	public static void main(String[] args) {

		Encryptor encrytor1 = new Encryptor();
		String msg = encrytor1.encrypt("Hello");
		System.out.println(msg);

		Encryptor encrytor2 = new DerivedEncryptor();
		msg = encrytor2.encrypt("Hello");
		System.out.println(msg);

	}

}

Output:

olleH
Jgnnq

 

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top