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
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 );
}
}
|
Output
Test.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
public class Test {
public static void foo() {
System.out.println( "Test.foo() called " );
}
public void 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
class Base {
public static void display() {
System.out.println( "Static or class method from Base" );
}
public void print() {
System.out.println( "Non-static or Instance method from Base" );
}
}
class Derived extends Base {
public static void display() {
System.out.println( "Static or class method from Derived" );
}
public void print() {
System.out.println( "Non-static or Instance method from Derived" );
}
}
public class Test {
public static void main(String args[ ]) {
Base obj1 = new Derived();
obj1.display();
obj1.print();
}
}
|
Output
Static 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
class Base {
public static void display()
{
System.out.println(
"Static or class method from Base" );
}
public void print()
{
System.out.println(
"Non-static or Instance method from Base" );
}
}
class Derived extends Base {
public void display()
{
System.out.println(
"Non-static method from Derived" );
}
public static void print()
{
System.out.println( "Static method from Derived" );
}
}
|
More about Static Methods refer to Static Methods in Java article.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
30 Jun, 2023
Like Article
Save Article