Can We Override Static Methods in Java?

Day 21 – Can We Override Static Methods in Java? 🤔 This is one of those classic trick questions every Java developer faces at least once — “Can we override static methods?” Let’s clear the confusion once and for all Short Answer: No, we cannot override static methods in Java. But wait — if we write the same static method in the subclass, Java doesn’t throw an error. So what’s happening there? What Actually Happens When a subclass defines a static method with the same name and signature as the one in the parent class — It’s called Method Hiding, not Overriding. Example: class Parent { static void display() { System.out.println("Static method from Parent"); } } class Child extends Parent { static void display() { System.out.println("Static method from Child"); } } public class Main { public static void main(String[] args) { Parent p = new Child(); p.display(); // Output: Static method from Parent } } 🧠 Why it Happens Because static methods belong to the class, not the object. Overriding is a runtime concept — it depends on the object type. Static methods are resolved at compile-time, based on the reference type. So in the example above, p.display() calls Parent.display() — even though p refers to a Child object. 💬 Interview Tip If the interviewer asks — “Can we override static methods?” You can confidently say: “No, static methods are hidden, not overridden — because they are resolved at compile time based on reference type, not object type.” ✅ ✨ Pro Tip: Try adding @Override before a static method — you’ll see the compiler won’t allow it. That’s your proof! #Java #100DaysOfJava #JavaInterview #StaticMethod #OOPs #MethodHiding #LearningJava #AshutoshTiwari #CleanCode #InterviewPreparation #Programming #Java #JavaDeveloper #CodingTips #SoftwareEngineering #CleanCode #TechCommunity #Java #Programming #SpringBoot #CodingTips #SoftwareEngineering #SoftwareDevelopment #JavaProgramming #CleanCode #CodingCommunity #BackendDevelopment #LearnToCode #ObjectOrientedProgramming #ProgrammingTips #HappyLearning #HappyCoding

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories