Hiding static methods in Java

Are you familiar with overriding methods in Java, but not sure what hiding static methods is? Well, in this article I'll explain what hidden static methods are, how to hide static methods, and what the difference is with overriding methods.

What are hidden static methods and how to hide static methods?

A hidden method occurs when a subclass defines a static method with the same signature as a static method in the superclass. Hiding static methods is similar but not exactly the same as overriding methods.

The following five rules must be followed to hide a static method:

  • The method in the subclass must have the same signature as the method in the superclass.
  • The method in the subclass must be at least as accessible or more accessible than the method in the superclass.
  • The method in the subclass may not throw a checked exception that is new or broader than the class of any exception thrown in the superclass method.
  • If the method returns a value, it must be the same or a subclass of the method in the superclass. This is also known as covariant return types.
  • If the method in the superclass is marked as static, then the method in the subclass must also be marked as static—this is method hiding. Likewise, the method must not be marked as static in the subclass if it is not marked as static in the superclass—this would be method overriding.

Example

Let's apply above rules in an example. In the following example, the code compiles and runs without an issue. The drive() method in the subclass hides the drive() method from the superclass. Note that this is not considered an overridden method, because both methods are marked as static.

public class Vehicle {
    public static void drive() {
        System.out.println("Vehicle is driving.")
    }
}
public class Car extends Vehicle {
    public static void drive() {
        System.out.println("Car is driving.");
    }

    public static void main(String[] args) {
        Car.eat();
    }
}

What is the difference with overriding a method?

When you override a method, a child method replaces the parent method in calls defined in both the parent and child. But when you hide a static method, the hidden method only replaces the parent methods in the class defined in the subclass.

Or, in the words of Oracle:

  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Helpful table

The following table can be helpful. It summarizes what happens when you define a method with the same signature as a method in a superclass:

Superclass instance method Super class static method
Subclass instance method Overrides Generates compile-time error
Sublcass static method Generates compile-time error Hides

What is the best practice?

In my opinion, I'd advise you to avoid hiding static methods in your code. I think it tends to lead to confusing and hard to read code. Therefore, you should not reuse the name of a static method in your subclass(es) if it is already used in the superclass.


SHARE THIS ARTICLE