Javatpoint Logo
Javatpoint Logo

Shadowing of Static Functions in Java

Shadowing of static functions in Java is the phenomenon of having two static methods in the same scope with the same name. The first method is said to be shadowed by the second method. The second method will take precedence over the first method when it is called.

When a static method is called from a class name or an object that has a class container, the method of the class is called, not the object's method.

Example 1:

Filename: SubClass.java

Output:

It is the static method in SuperClass
It is the static method in SubClass

Example 2:

Filename: StaticFunction.java

Output:

Father

Explanation:

In method overloading, if a superclass and subclass have a method with the same signature, both methods are available to an object of the subclass. The method that is executed depends on the type of the object reference.

In the case of static methods, since they do not belong to any instance, they are accessed using the class name. If a superclass and subclass have static methods with the same signature, the subclass method will shadow the superclass method. It means that the subclass method will be executed instead of the superclass method, even if the object reference is of type SuperClass.

Overriding is not possible with static methods because static methods are resolved at compile time.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA