Can we Overload or Override static methods in java ? - GeeksforGeeks

01 Sep, 2021
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 signatures of both methods must be the same. Refer 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 that in 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.javapublic 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);    }}
Test.foo() called 
Test.foo(int) called 
Can we overload methods that differ only by static keyword? We cannot overload two methods in Java if they differ only by static keyword (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.javapublic 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. */ // Superclassclass 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");    }} // Subclassclass 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 classpublic 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();        }}
Static or class method from Base
Non-static or Instance method from Derived
The following are some important points for method overriding and static methods in Java. 1) 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.2) 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.3) 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.  
Java
/* Java program to show that if static methods are redefined by   a derived class, then it is not overriding but hidding. */ // Superclassclass 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");    }} // Subclassclass 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");   }}
4) 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.
References: http://docs.oracle.com/javase/tutorial/java/IandI/override.htmlThis article is contributed by Chandra Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
ashok3612
shivam2608
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples
Reverse a string in Java
HashMap in Java with Examples
Interfaces in Java
Stream In Java
How to iterate any Map in Java