What are hidden methods in Java?

When a subclass defines a static method with the same header as a static method in the superclass, the method definition in the subclass is said to hide the definition in the superclass. The concepts of hiding and overriding are quite similar:

  • Hiding involves static methods.
    • The compiler decides which static method to call.
  • Overriding involves instance methodsthose that are not static.
    • The Java Virtual Machine (JVM) decides whether to call the overriding method in the subclass or the overridden method in the superclass.

For example, consider the following two simple classes:

public class BaseClass
{
   public void objectAction()       // Instance method
   {
      System.out.println("objectAction in BaseClass.");
   } // End objectAction

   public static void classAction() // Static method
   {
      System.out.println("classAction in BaseClass.");
   } // End classAction
} // End BaseClass
public class DerivedClass extends BaseClass
{
   public void objectAction()       // Instance method
   {
      System.out.println("objectAction in DerivedClass.");
   } // End objectAction

   public static void classAction() // Static method
   {
      System.out.println("classAction in DerivedClass.");
   } // End classAction
} // End DerivedClass

Given these classes and the statement,

DerivedClass derivedObject = new DerivedClass();

the following actions occur:

  • derivedObject.objectAction() invokes DerivedClass’s method objectAction

  • DerivedClass.classAction() invokes DerivedClass’s method classAction

  • derivedObject.classAction() also invokes DerivedClass’s method classAction

You can confirm these results by running the following program:

Demo.java
DerivedClass.java
BaseClass.java
public class Demo
{
public static void main(String[] args)
{
DerivedClass derivedObject = new DerivedClass();
System.out.print("derivedObject.objectAction() calls ");
derivedObject.objectAction();
System.out.print("DerivedClass.classAction() calls ");
DerivedClass.classAction();
System.out.print("derivedObject.classAction() calls ");
derivedObject.classAction();
} // End main
} // End Demo

Conclusions:

  • The instance method objectAction in DerivedClass overrides the instance method objectAction in BaseClass.

  • The static method classAction in DerivedClass hides the static method classAction in BaseClass.

✏️ Note

  • Only a static method in a subclass can hide another static method in a superclass
  • Only an instance method in a subclass can override another instance method in a superclass

That is, when hiding or overriding methods, you cannot add or delete the modifier static in a method’s heading.

✏️ Note

The preferable way to invoke a static method is to use the class name instead of an object.

Once a method is hidden, can you invoke it? Yes, you can. Given the previous classes and definitions, the way to call the hidden method classAction in BaseClass is with the statement,

BaseClass.classAction();

What happens if you define the following alias for derivedObject?

BaseClass baseObjectAlias = derivedObject;

The following actions occur:

  • baseObjectAlias.classAction() invokes BaseClass’s method classAction

    • This action occurs because the method is static. Again, you should use the class name—BaseClass—instead of an object name to call a static method.
  • baseObjectAlias.objectAction() invokes DerivedClass’s method objectAction

    • Since the alias references a DerivedClass object, the object’s method is called.

You can confirm these results by running the following program:

Demo.java
DerivedClass.java
BaseClass.java
public class Demo
{
public static void main(String[] args)
{
DerivedClass derivedObject = new DerivedClass();
BaseClass baseObjectAlias = derivedObject;
System.out.print("baseObjectAlias.objectAction() calls ");
baseObjectAlias.objectAction();
System.out.print("BaseClass.classAction() calls ");
BaseClass.classAction();
System.out.print("baseObjectAlias.classAction() calls ");
baseObjectAlias.classAction(); } // End main
} // End Demo

✏️ Note

A subclass can overload a method it inherits from its superclass. The subclass method neither hides nor overrides the inherited method.

✏️ Note

  • A static method in a subclass cannot use super to invoke a hidden method defined in the superclass.

  • However, an instance method in a subclass can use super to invoke an overridden the method defined in the superclass.

✏️ Note

When a method in a subclass either hides or overrides a method in the superclass, its access modifier cannot restrict the access of the hidden or overridden method. That is, it must provide either the same or more access.