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 methods 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 non-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 displayValue() 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.
public class StaticOverload { public static void displayValue(int i){ System.out.println("Value is- " + i); } public static void displayValue(int i, int j){ System.out.println("Values are- " + i + " " + j); } public static void displayValue(int i, String s){ System.out.println("Values are- " + i + " " + s); } public static void main(String[] args) { //displayValue(int i) StaticOverload.displayValue(5); //displayValue(int i, int j) StaticOverload.displayValue(5, 7); //displayValue(int i, String s) StaticOverload.displayValue(5, "Hello"); } }Output
Value is- 5 Values are- 5 7 Values are- 5 Hello
Rules for static method overloading in Java
- If static methods differ only in return type those are not considered as overloaded methods.
- 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”
public void displayValue(int i){ System.out.println("Value is- " + i); } public static void displayValue(int i){ System.out.println("Values are- " + i); }
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.
class Parent{ //static method public static void cantOverride(){ System.out.println("This static method can not be overridden"); } // non-static method public void canOverride(){ System.out.println("This method can be overridden"); } } class Child extends Parent{ //static method public static void cantOverride(){ System.out.println("Static method in child class"); } // non-static method public void canOverride(){ System.out.println("This method is overridden in child class"); } } public class StaticOverride { public static void main(String[] args) { Parent obj = new Child(); obj.cantOverride(); obj.canOverride(); } }Output
This static method can not be overridden This method is overridden in child class
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
No comments:
Post a Comment