In this guide, we will discuss whether we can overload or override a static method in Java.
Prerequisites: To understand this article, you should have the basic knowledge of following topics.
Can we overload a static method?
Short answer is ‘Yes’. We can overload a static method. In the following example, we are overloading a static method myMethod()
.
public class JavaExample { //static method overloading public static void myMethod() { System.out.println("Hello, Welcome to beginnersbook.com!"); } public static void myMethod(String str) { System.out.println("Hello "+str+ " glad you are here."); } public static void main(String args[]) { JavaExample.myMethod(); JavaExample.myMethod("Chaitanya"); } }
Output:
Hello, Welcome to beginnersbook.com! Hello Chaitanya glad you are here.
Can we overload a non-static method with a static method?
If both the methods have same signature but one method is non-static and the other method is static then, this is not a valid case of method overloading and it would throw a compilation error.
public class JavaExample { //static method public static void myMethod() { System.out.println("Static Method."); } //non-static method with the same name and parameters public void myMethod() { System.out.println("Regular Method."); } public static void main(String args[]) { //calling method JavaExample obj = new JavaExample(); obj.myMethod(); } }
Output:
Can we Override static methods in Java?
No, we cannot override a static method. However when we try to override a static method, the program runs fine without any compilation error, it is just that the overriding doesn’t take place. Instead of calling the derived class method, the compiler invokes the base class static method, it is because static methods cannot be overriden.
You can say that when we define a static method with the same signature in derived class which is present in the base class, then the static method of base class hides the same signature method of derived class, this concept is called method hiding.
// Base Class class Parent { // Static method of base class public static void hello() { System.out.println("Base class says hello!"); } // Non-static method of base class public void bye() { System.out.println("Base class says bye!"); } } // Derived class class Child extends Parent { // Here we are trying to override the hello() method of base // class, however it cannot be done, thus this method will be hidden // and cannot be called. public static void hello() { System.out.println("Child class says hello!"); } // Overriding non-static method of base class public void bye() { System.out.println("Child class says bye!"); } } //Main class public class JavaExample { public static void main(String args[ ]) { Parent obj = new Child(); // Unlike normal methods, static methods can't be overriden //so this would call the static method of base class obj.hello(); // For normal methods, overriding is perfectly fine. //This should call the method of derived class obj.bye(); } }
Output: