Can we override the static method in Java?



Yes! We can override the static method easily in Java. See the example below.

Example

Live Demo

public class Tester {
   public static void main(String[] args) {
      display();
      display("World!!");
   }
 
   private static void display(){
      System.out.println("Hello!");
   }
 
   private static void display(String message){
      System.out.println("Hello! " + message);
   }
}

Output

It will print the following lines −

Hello!
Hello! World!!

Advertisements