Can You Override Static Methods in Java? Not Quite. Here’s Why.

πŸ“Ί Watch This Explanation in 15 Seconds!

If you're learning Java or preparing for interviews, you've probably heard that everything in Java is an object. But what happens when you try to override a static method? The result can be surprisingly misleading.

In this post, I’ll walk you through a common misconception about static methods in Java — that they can be overridden. Spoiler alert: they can’t. But don’t worry — we’ll break it down simply, with code examples that clarify exactly what’s going on.

πŸ“Œ Static Methods: A Quick Refresher

In Java, a static method belongs to the class, not the instance of the class. This means you don’t need to create an object to call it:

class Utility {
    public static void sayHello() {
        System.out.println("Hello, world!");
    }
}

// Usage
Utility.sayHello();

🚨 The Static Override Illusion

Let’s say you have a parent class and a child class, both defining a static method with the same name:

class Parent {
    public static void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    public static void greet() {
        System.out.println("Hello from Child");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.greet();
    }
}

Expected output? Most people think it should be Hello from Child.

Actual output? Hello from Parent.

⚠️ Why Doesn’t It Call the Child’s Method?

The key point: Java does not apply polymorphism to static methods.

When you call a static method, the reference type — not the object’s actual type — determines which method is called.

Parent obj = new Child();  // Reference is of type Parent
obj.greet();              // Calls Parent.greet(), not Child.greet()

This is known as method hiding, not method overriding.

🧠 How Is This Different from Instance Methods?

Let’s contrast it with instance methods, where polymorphism does apply:

class Parent {
    public void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    public void greet() {
        System.out.println("Hello from Child");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.greet();  // Output: Hello from Child
    }
}

Since greet() is now an instance method, Java looks at the object’s actual type (Child) at runtime and invokes Child.greet().

✅ Takeaway

  • You cannot override static methods in Java.
  • Static methods are class-level, and method selection happens at compile time, based on the reference type.
  • Defining a static method with the same name in a child class hides the parent’s method — it doesn’t override it.

πŸ§ͺ Bonus Tip for Interviews

Interviewers love this question because it exposes whether you understand the difference between method hiding and method overriding. Be sure to clarify your answer with examples when explaining it — just like we did here.

πŸ“Œ Final Thought

Java has a few traps like this that can easily trip up even experienced developers. Knowing how Java really works under the hood helps you write safer, cleaner code — and stand out in interviews.

πŸ‘‰ Follow Me for more Java tips, interview tricks, and coding gotchas explained clearly and practically.

Comments

Popular posts from this blog

Docker for Beginners: Build, Ship, and Run Apps with Ease

Top 7 Real-World Projects to Learn React, Python, and AWS (Beginner to Advanced)