Skip to content
Related Articles

Related Articles

Improve Article

Shadowing of static functions in Java

  • Difficulty Level : Easy
  • Last Updated : 19 Feb, 2021
Geek Week

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 “” 
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. 


// file name:
// Parent class
class A {
    static void fun() { System.out.println(""); }
// B is inheriting A
// Child class
class B extends A {
    static void fun() { System.out.println(""); }
// Driver Method
public class Main {
    public static void main(String args[])
        A a = new B();; // prints;
        // B a = new B();
        //; // prints
        // the variable type decides the method
        // being invoked, not the assigned object type

Note: If we make both and as non-static then the above program would print “”. While both methods are static types, the variable type decides the method being invoked, not the assigned object type
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :