LeetCampus
Interview Question

Can the static methods be overridden?

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Can static methods be overridden? This question tests the candidate's understanding of static methods in programming languages like Java and C#. Static methods belong to the class rather than to any specific instance of the class. Therefore, they are not subject to polymorphism, which is the core principle behind method overriding. Interviewers ask this question to gauge the candidate's grasp of object-oriented programming concepts, particularly how static methods differ from instance methods. Understanding this distinction is crucial because it affects how classes interact and how code can be structured. Common misconceptions include believing that static methods can behave like instance methods, leading to confusion in inheritance scenarios. By clarifying these points, candidates demonstrate their foundational knowledge in software engineering and their ability to apply these concepts in real-world applications. This knowledge is vital for effective coding practices, especially in large codebases where clarity and maintainability are paramount.

Sample Answers

Example 1: Static Methods and Overriding

In programming languages like Java, static methods cannot be overridden in the same way instance methods can. When a subclass defines a static method with the same name and parameters as a static method in the superclass, the method in the subclass hides the method in the superclass rather than overriding it. This means that the method invoked will depend on the reference type, not the object type. For example:

class Parent {
    static void display() {
        System.out.println("Parent static method");
    }
}

class Child extends Parent {
    static void display() {
        System.out.println("Child static method");
    }
}

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

In this example, even though obj is an instance of Child, the display() method from Parent is called. This highlights the key concept that static methods are resolved at compile time based on the reference type.

Example 2: Hiding vs. Overriding

To further illustrate the concept of static method hiding, consider the following example. Imagine we have a base class Animal and a derived class Dog:

class Animal {
    static void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    static void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.sound();  // Output: Animal sound
    }
}

Here, the sound() method in Dog hides the sound() method in Animal. When we call myAnimal.sound(), it invokes the sound() method in Animal, not Dog. This demonstrates the crucial difference between hiding and overriding: the method resolution for static methods is based on the reference type rather than the actual object type, which is a fundamental aspect of static method behavior.

Example 3: Practical Implications

Understanding that static methods cannot be overridden but can be hidden has significant implications in software design. For instance, when designing a framework, if you mistakenly assume that static methods can be overridden, you might introduce bugs that are hard to trace. This can lead to unexpected behaviors in your applications, especially when dealing with class hierarchies. Consider a logging framework that uses static methods for logging levels:

class Logger {
    static void logInfo() {
        System.out.println("Info log");
    }
}

class CustomLogger extends Logger {
    static void logInfo() {
        System.out.println("Custom info log");
    }
}

public class Application {
    public static void main(String[] args) {
        Logger logger = new CustomLogger();
        logger.logInfo();  // Output: Info log
    }
}

In the scenario above, even though logger is an instance of CustomLogger, the logInfo() method from Logger is what gets executed. This can cause confusion for developers working with your code, emphasizing the need for clear documentation and understanding of static method behaviors.

Keywords

static methodsmethod overridingobject-oriented programminginheritancesoftware engineering

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions