Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Can We Override Static Method in Java?

Last Updated on December 27, 2023 by Ankit Kochar

Java, as an object-oriented programming language, provides a range of features to facilitate inheritance and method overriding. However, when it comes to static methods, the rules are slightly different. Static methods in Java belong to the class rather than to an instance, leading to a common query among developers: "Can we override static methods in Java?"
This article delves into the intricacies of static methods, their behavior in inheritance, and whether it’s possible to override them in Java. By understanding the fundamental concepts behind method overriding and the specific characteristics of static methods, we aim to clarify this frequently debated topic in Java programming.

Static Method in Java

In Java, Static Method is the one that is associated with a class rather than being associated with the instance of the class. The Static Methods can be called even without creating the instance or object of a class which means, for accessing a static object, we only need the class name. To declare a static method, we use the “static” keyword. The static method in Java is demonstrated in the following example.

Code for Static Method in Java:

class MyClass {
    public static void myStaticMethod() {
        System.out.println("This is a static method.");
    }
}
public class PrepBytes{
    public static void main(String args[]){
        MyClass.myStaticMethod();
    }
}

Output:

This is a static method.

Here, in the above, we have declared a static method named “myStaticMethod” in a class named, “MyClass”. We can access this static method, without creating the object using the statement “MyClass.myStaticMethod()”. We have used this statement in the main method, and on the output screen, we get “This is a static method.”, which gets printed after the execution of the static method.

Notice here that we have not created any object of class “MyClass”, but accessed the method directly, since it is a static method.

Can We Override Static Method in Java?

The short answer is No. Static methods in Java cannot be overridden. This is because static methods are not associated with the instance of a class, but with the class itself. Therefore, when a subclass inherits a static method from its parent class, it cannot modify the behavior of the static method in any way.

To better understand this concept of overriding static methods in java, consider the following example:

Example to Prove that Static Method cannot be Overridden in Java

This example will answer whether can we Override the Static Method in Java or not.

Code:

class Parent {
    public static void staticMethod() {
        System.out.println("Parent's static method");
    }
}


class Child extends Parent {
    public static void staticMethod() {
        System.out.println("Child's static method");
    }
}


public class PrepBytes {
    public static void main(String[] args) {
        Parent p = new Child();
        p.staticMethod();
    }
}

Output:

Parent's static method

Explanation:
In the above example, we have a Parent class that defines a static method called staticMethod(). We also have a Child class that inherits from the Parent class and also defines a static method called staticMethod(). Finally, we have a PrepBytes class that creates an instance of the Child class and assigns it to a Parent class reference variable.

When we call the staticMethod() method on the variable p, it should ideally call the static method of the Child class, since p is referring to an instance of the Child class. However, since static methods cannot be overridden, it will instead call the static method of the Parent class, and the output will be Parent’s static method.

Data Hiding in Java

It is not possible to override the static methods in Java. However, it is possible to hide static methods in Java using the static keyword. Let us learn about the concept of Data Hiding in Java in detail.

Code:

class Parent {
    public static void staticMethod() {
        System.out.println("Parent's static method");
    }
}


class Child extends Parent {
    public static void staticMethod() {
        System.out.println("Child's static method");
    }
}


public class PrepBytes {
    public static void main(String[] args) {
        Parent p = new Parent();
        Child c = new Child();
        p.staticMethod();
        c.staticMethod();
    }
}

Output:

Parent's static method
Child's static method

Explanation:
Here in the above code, we have defined three classes:

  • “Parent” Class: It has a static method named, “staticMethod()”.
  • “Child” Class: This class is derived from the “Parent” Class. This class also defines a static method called “staticMethod()” with the same signature same as the parent class.
  • “PrepBytes” Class: This is the class containing the main method.

In the “PrepBytes” Class, when we create the object of class “Parent” Class and call the “myStaticMethod()”. This will call the static method defined in the Parent Class and prints the line “Parent’s static method” on the screen.

Similarly, when we create an object of class “Child” and call the static method named “myStaticMethod()”. This statement will call the method which is defined in the “Child” class and on the output screen, we see “Child’s static method”.

This shows that we can hide a static method in a subclass, but the behavior of the original static method is still present in the patent class. Although the behavior of the static method of the parent class is not visible in the parent class, the subclass has its own implementation of the static method.

Conclusion
In conclusion, the concept of method overriding in Java is a powerful mechanism that facilitates polymorphism and dynamic method resolution at runtime. However, when dealing with static methods, overriding works differently due to their association with the class rather than an instance. Java does not allow true method overriding for static methods.

While you can declare a static method with the same signature in a subclass, it doesn’t override the method from the superclass but hides it instead. The choice of which method to invoke is determined at compile time, based on the reference type.

Understanding these distinctions is crucial for Java developers to write efficient and maintainable code while leveraging the language’s features effectively.

Frequently Asked Questions (FAQs) on Override Static Method in Java

Some Frequently Asked Questions on “Can we override static method in Java” are given below.

1. Why can’t we override static methods in Java?
Static methods belong to the class, not to individual instances. They are resolved at compile time based on the reference type, not the runtime object type. Hence, overriding (as in instance methods) does not apply to static methods in Java.

2. What happens if I declare a static method in a subclass with the same signature as in the superclass?
While it’s possible to declare a static method with the same signature in a subclass, it’s not considered overriding. Instead, it hides the superclass method. The method called depends on the reference type at compile time.

3. Can I use the @Override annotation for static methods in Java?
No, the @Override annotation is not applicable to static methods. It’s used to indicate that a method in a subclass overrides a method in the superclass, which is not the case for static methods.

4. How should I access a static method defined in a superclass from a subclass?
You can access a static method defined in a superclass directly using the superclass name followed by the method name, like SuperClass.staticMethod(), regardless of inheritance.

5. Are static methods inherited in Java?
Yes, static methods are inherited in Java, but they are not overridden. Subclasses have access to static methods from their superclass, but redeclaring a static method with the same signature in the subclass creates a new method that hides the superclass method.

6. Can I override a static method with a non-static method or vice versa?
No, it’s not possible to override a static method with a non-static method or vice versa in Java. Method overriding must maintain the same method signature, including the static or non-static nature, which is not allowed when switching between these method types.

7. How does method invocation differ between static and instance methods in Java?
Instance methods are invoked based on the runtime type of the object, allowing for dynamic dispatch, whereas static methods are resolved at compile time based on the reference type. This means instance methods support polymorphism and method overriding, while static methods do not.

Leave a Reply

Your email address will not be published. Required fields are marked *