Shadowing of static functions in Java
Last Updated :
19 Feb, 2021
In Java, if the name of a derived class static function is the same as a base class static function then the base class static function shadows (or conceals) the derived class static function. For example, the following Java code prints “A.fun()”
Note: Static method is a class property, so if a static method is called from a class name or object having a class container then the method of that class is called not the object’s method.
Java
class A {
static void fun() { System.out.println( "A.fun()" ); }
}
class B extends A {
static void fun() { System.out.println( "B.fun()" ); }
}
public class Main {
public static void main(String args[])
{
A a = new B();
a.fun();
}
}
|
Note: If we make both A.fun() and B.fun() as non-static then the above program would print “B.fun()”. While both methods are static types, the variable type decides the method being invoked, not the assigned object type
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...