Can we Overload or Override static methods in java ?
Last Updated :
30 Jun, 2023
Let us first define Overloading and Overriding.
Overriding: Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in the superclass (or base class).
The implementation to be executed is decided at run-time and a decision is made according to the object used for the call. Note that the signatures of both methods must be the same. Refer to Overriding in Java for details.
Overloading: Overloading is also a feature of OOP languages like Java that is related to compile-time (or static) polymorphism. This feature allows different methods to have the same name, but different signatures, especially the number of input parameters and type of input parameters.
Note: Both C++ and Java, methods cannot be overloaded according to the return type.
Can we overload static methods?
The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.
For example, consider the following Java program.
Java
// filename Test.java
public class Test {
public static void foo() {
System.out.println("Test.foo() called ");
}
public static void foo(int a) {
System.out.println("Test.foo(int) called ");
}
public static void main(String args[])
{
Test.foo();
Test.foo(10);
}
}
OutputTest.foo() called
Test.foo(int) called
Can we overload methods that differ only by static keywords?
We cannot overload two methods in Java if they differ only by static keyword (the number of parameters and types of parameters is the same).
See the following Java program for example. This behavior is the same in C++ (See point 2 of this).
Java
// filename Test.java
public class Test {
public static void foo() {
System.out.println("Test.foo() called ");
}
public void foo() { // Compiler Error: cannot redefine foo()
System.out.println("Test.foo(int) called ");
}
public static void main(String args[]) {
Test.foo();
}
}
Output:
Compiler Error, cannot redefine foo()
Can we Override static methods in Java?
We can declare static methods with the same signature in the subclass, but it is not considered overriding as there won't be any run-time polymorphism. Hence the answer is 'No'.
If a derived class defines a static method with the same signature as a static method in the base class, the method in the derived class is hidden by the method in the base class.
Java
/* Java program to show that if static method is redefined by
a derived class, then it is not overriding. */
// Superclass
class Base {
// Static method in base class which will be hidden in subclass
public static void display() {
System.out.println("Static or class method from Base");
}
// Non-static method which will be overridden in derived class
public void print() {
System.out.println("Non-static or Instance method from Base");
}
}
// Subclass
class Derived extends Base {
// This method is hidden by display() in Base
public static void display() {
System.out.println("Static or class method from Derived");
}
// This method overrides print() in Base
public void print() {
System.out.println("Non-static or Instance method from Derived");
}
}
// Driver class
public class Test {
public static void main(String args[ ]) {
Base obj1 = new Derived();
// As per overriding rules this should call to class Derive's static
// overridden method. Since static method can not be overridden, it
// calls Base's display()
obj1.display();
// Here overriding works and Derive's print() is called
obj1.print();
}
}
OutputStatic or class method from Base
Non-static or Instance method from Derived
Important Points about method overriding and static methods
The following are some important points for method overriding and static methods in Java.
- For class (or static) methods, the method according to the type of reference is called, not according to the object being referred, which means method call is decided at compile time.
- For instance (or non-static) methods, the method is called according to the type of object being referred, not according to the type of reference, which means method calls is decided at run time.
- An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.
- In a subclass (or Derived Class), we can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods — they are new methods, unique to the subclass.
Example:
Java
// Java program to show that if static methods are redefined
// by a derived class, then it is not overriding but
// hidding.
// Superclass
class Base {
// Static method in base class which will be hidden in
// subclass
public static void display()
{
System.out.println(
"Static or class method from Base");
}
// Non-static method which will be overridden in derived
// class
public void print()
{
System.out.println(
"Non-static or Instance method from Base");
}
}
// Subclass
class Derived extends Base {
// Static is removed here (Causes Compiler Error)
public void display()
{
System.out.println(
"Non-static method from Derived");
}
// Static is added here (Causes Compiler Error)
public static void print()
{
System.out.println("Static method from Derived");
}
}
More about Static Methods refer to Static Methods in Java article.
Similar Reads
Java Methods
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Parameter Passing Techniques in Java with Examples
There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the "caller function" and B is called the "called function or callee function". Also, the arguments wh
4 min read
Java is Strictly Pass by Value!
In order to understand more of how the java is processing the parameter in methods and functions, lets compare the java program with a C++ code which would make it more clear and helps you get the major difference between how the parameters are being passed to any methods or functions wrt passing pa
6 min read
How are parameters passed in Java?
See this for detailed description. In Java, parameters are always passed by value. For example, following program prints i = 10, j = 20. java // Test.java public class Test { // swap() doesn't swap i and j public static void swap(Integer i, Integer j) { Integer temp = new Integer(i); i = j; j = temp
1 min read
Method overloading and null error in Java
In Java it is very common to overload methods. Below is an interesting Java program. Java public class Test { // Overloaded methods public void fun(Integer i) { System.out.println("fun(Integer ) "); } public void fun(String name) { System.out.println("fun(String ) &quo
2 min read
Can we Overload or Override static methods in java ?
Let us first define Overloading and Overriding. Overriding: Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in the superclass (or base class). The implementation to be executed i
5 min read
Access specifier of methods in interfaces
In Java, all methods in an interface are public even if we do not specify public with method names. Also, data fields are public static final even if we do not mention it with fields names. Therefore, data fields must be initialized. Consider the following example, x is by default public static fina
1 min read
Java main() Method - public static void main(String[] args)
Java's main() method is the starting point from where the JVM starts the execution of a Java program. JVM will not execute the code if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.The Java co
6 min read
Is main method compulsory in Java?
The answer to this question depends on the version of java you are using. Prior to JDK 7, the main method was not mandatory in a java program. You could write your full code under static block and it ran normally. The static block is first executed as soon as the class is loaded before the main(); t
2 min read
Understanding "static" in "public static void main" in Java
Following points explain what is "static" in the main() method: main() method: The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method. Static is a keyword. The role of adding static before an
3 min read