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.

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.

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.

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,



Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.