Can we override the static method in Java?

Java Programming Java8Object Oriented Programming

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!!
raja
Published on 23-Apr-2018 00:24:44
Advertisements