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?

Java is a popular object-oriented programming language. This language is widely used for the development of a wide range of applications for large enterprises. Java provides the feature of static functions. Static methods are methods that can be accessed directly without creating an object of the class. And, Overriding is the process in Java that allows the child class to have a unique implementation of the method already defined in the Parent Class. Now, the question arises of that can we override Static Method in Java.

In this article, we will try to answer whether can we override Static Method in Java. Before, answering the question, let us first learn about static Methods in Java.

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, 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. However, it is possible to hide static methods in a subclass using the static keyword. When a subclass defines a static method with the same name and signature as the static method in its parent class, it is said to be hiding the static method of the parent class. The behavior of the original static method is still present in the parent class, but it is not visible through the subclass. Instead, the subclass has its own implementation of the static method.

Frequently Asked Questions (FAQs)

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

Ques 1. What is the difference between method overriding and method hiding?
Ans. Method overriding is defined as a process when a derived class changes the predefined method for implementing some unique features different from the parent class, whereas when a derived class changes the predefined static method, it is called as method hiding in Java.

Ques 2. Can we call a static method from an instance method in Java?
Ans. Yes, we can call a static method from an instance method, but we must use the class name to refer to the static method.

Ques 3. What happens if we try to override a static method in Java?
Ans. If we try to override a static method in java, the method present in the subclass will hide the method present in the superclass. This process is also known as Data Hiding.

Ques 4. What is the access level of a static method in Java?
Ans. The access level of a static method is determined by its access modifier, just like any other method in Java.

Ques 5. Can we declare a static method as final in Java?
Ans. Yes, we can declare a static method as final in Java.

Leave a Reply

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