Can We Override Static Method in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Can We Override Static Method in Java?

NO, we can't override static methods since method overriding relies on dynamic binding at runtime, but static methods are bonded at compile time with static binding. As a result, we are unable to override static methods.

If we try to override the static method in the child class then the child's class method will get hidden and the parent's class method will be called based on the object reference.

Some Terminologies

  • Static Method Also known as class level method and it is declared using a static keyword, its copy is shared by all the objects of a class.

    A static method can be called by using the class name, for example, Math.max(a, b) is a static method in the Math class that returns a maximum of the two values passed into it.

  • Method Overloading It is used to achieve compile time polymorphism. Method overloading occurs when a class contains multiple methods with the same name but distinct signatures. Which method will be called is decided at the compile time itself using the help of parameters passed to them.

  • Method Overriding It is used to achieve run-time polymorphism. Method overriding comes into the picture when there is inheritance, the subclass provides a specific implementation of a method that is already given by its parent class. The method signatures in the parent and child classes must be identical. The method to be executed in method overriding is determined at run-time based on the object that we referred to.

Example to Prove that Static Methods Cannot Be Overriden

If static methods are redefined by a derived class, then it is not method overriding but method hiding.

Output:

Explanation:

  • In the above code, there are three classes namely Base, Derived, and Driver.
  • In the Base and Derived classes there are two methods add() (non-static) and print() (static).
  • Since add() is a non-static method it will be overridden in the child class but the print() method is static and hence it will be not.
  • So when we make an object of the Base class in the Driver class and make it refer to the child class Derived, then due to overriding rules add() method of child class is called.
  • Since print() is a static method it didn't get override due to which Base class print() is called.

Important Points for Method Overriding and Static Methods in Java

  • For static methods, the method is called based on the type of reference, not the object being referred to, implying that the method call is determined at compile time.
  • For non-static methods, the method is called based on the type of object being referenced, rather than the type of reference, implying that method calls are made at runtime.
  • A static method cannot hide an instance method, and an instance method cannot override it. The following program illustrates this.

Output:

Explanation:

  • In the above code, there are three classes namely Base, Derived, and Driver. In the Base class, there are two methods add() (non-static) and print() (static).
  • In the Derived class there are two methods, add() which we have tried to make static but it causes compilation error because a static method cannot hide an instance method add() of the Base class.
  • The print() method which is an instance method tries to override print() method of Base class which is static causes another compilation error because an instance method cannot override a static method.
  • We can overload the methods inherited from the superclass in a subclass (or Derived Class). These overloaded methods don't hide or override the superclass methods; they're new, subclass-specific methods as they have a different signature.

Conclusion

  • Static methods cannot be overridden since they are bonded at compile time and method overriding relies on dynamic binding at runtime.
  • If static methods are redefined by a derived class, then it is not Method Overriding but Method Hiding.
  • Static method cannot hide an instance method, and an instance method cannot override a static method.