Static Method Overloading And Overriding in Java

When a class member is defined using static keyword in Java it is associated with the class rather than with the instances of the class. That gives rise to a question can we overload or override static method in Java. This post tries to answer this question.

Before that let’s do a quick recap of method overloading and method overriding in Java-

Method overloading– In Java you can have two or more methods having the same name with in the same class provided their arguments differ in either type or number. These types of methods are called overloaded methods and the process is known as method overloading in Java.

Method overriding– When a method in child class has the same name and the same signature (same number and type of parameters) as the method in the parent class then the sub class is overriding the method in the super class and this process is known as method overriding in Java.

Static method overloading and overriding in Java

Now the question is what happens with in the static context. Can we overload a static method or override a static method in Java or not?

Static method overloading in Java

Overloading a static method is possible just like in the case of static methods. You can have two or more static methods having the same name, but different parameters in terms of parameter types or numbers.

Java static method overloading example

In the example there is an overloaded static method dispalyValue() where one method is defined with a single int parameter, another one takes two int parameters and yet another takes one int and one String parameter.

Output

Rules for static method overloading in Java

  1. If static methods differ only in return type those are not considered as overloaded methods.
  2. If you have an instance method and a static method having the same name then it is not considered as overloading, in fact it results in compile time error.
    Consider the following code where one of the method is static. This code gives compile time error “Duplicate method displayValue(int) in type”

Static method overriding in Java

Static method overriding is not possible. If you have a static method with same signature in the child class as in parent class it merely hides the parent class method. There won’t be any run time polymorphism i.e. you won’t be able to call child class overridden static method using the parent class reference as static methods are bound during compile time itself, they are not resolved at run time.

Output

As you can see even if the parent class reference holds the child class object on calling the overridden static method, method of the parent class is called where as for the non-static method child class method is called.

That’s all for the topic Static Method Overloading And Overriding in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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.