In Java, if the name of a derived class static function is that the same as a base class static function then the bottom class static function shadows (or conceals) the derived class static function. for instance , the subsequent Java code prints “A.fun()”
Note: Static method may be a class property, so if a static method is named from a category name or object having a category container then the tactic of that class is named not the object’s method.
// file name: Main.java // Parent class class A { static void fun() { System.out.println("A.fun()"); } } // B is inheriting A // Child class class B extends A { static void fun() { System.out.println("B.fun()"); } } // Driver Method public class Main { public static void main(String args[]) { A a = new B(); a.fun(); // prints A.fun(); // B a = new B(); // a.fun(); // prints B.fun() // the variable type decides the method // being invoked, not the assigned object type } }
Output
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 tactic being invoked, not the assigned object type
Please write comments if you discover anything incorrect, otherwise you want to share more information about the subject discussed above.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.