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 methods those 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()invokesDerivedClass’s methodobjectAction -
DerivedClass.classAction()invokesDerivedClass’s methodclassAction -
derivedObject.classAction()also invokesDerivedClass’s methodclassAction
You can confirm these results by running the following program:
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
objectActioninDerivedClassoverrides the instance methodobjectActioninBaseClass. -
The static method
classActioninDerivedClasshides the static methodclassActioninBaseClass.
✏️ 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
staticin 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()invokesBaseClass’s methodclassAction- 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.
- This action occurs because the method is static. Again, you should use the class name—
-
baseObjectAlias.objectAction()invokesDerivedClass’s methodobjectAction- Since the alias references a
DerivedClassobject, the object’s method is called.
- Since the alias references a
You can confirm these results by running the following program:
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
superto invoke a hidden method defined in the superclass.However, an instance method in a subclass can use
superto 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.