Can we override static method?

Ans

Override:

Overriding is a feature of opp language that is related to run-time polymorphism. Overriding signature of both methods must be same.

We can declare static method with same signature in subclass, but it is not considering overriding as there won’t be any run-time polymorphism, Hence we can’t override static method.

If a derived class define static method with same signature as a static method in base class, that method in derived class hide the method in the base class.

Example:

class Base
{
 public static void dis()
 {
  System.out.println("print base static display");
 }
 public void print()
 {
  System.out.println("print non-static display");
 }
}
class Derived extends Base
{
 public static void dis()
 {
  System.out.println("print derived display");
 }
 public void print() 
 {
 System.out.println("print non-static derived display"); 
 }
}

public class Test {
 

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Base obj1 = new Derived();
  obj1.dis();
  obj1.print();
   
 }

}

Output:

print base static display
print non-static derived display