I read on all the forums so many times that Static methods cannot be inherited and cannot be overridden. But I came across the code which is allowing me to override and inherit static methods. Please go through the code below for overriding the static method.
package foo;
class A{
A(){
System.out.println("Here is A's constructor");
}
public static void print()
{
System.out.println("Printing A");
}
}
public class B extends A{
B(){
System.out.println("Here is B's constructor");
}
public static void print()
{
System.out.println("Printing B");
}
public static void main(String[] args){
B b = new B();
B.print();
A.print();
}
}
The output I got for this was allowing me to override this method print. It worked fine. Please let me know if it is really possible to override and inherit static method.
package foo;
class A{
A(){
System.out.println("Here is A's constructor");
}
public static void print()
{
System.out.println("Printing A");
}
}
public class B extends A{
B(){
System.out.println("Here is B's constructor");
}
public static void print()
{
System.out.println("Printing B");
}
public static void main(String[] args){
B b = new B();
B.print();
A.print();
}
}
The output I got for this was allowing me to override this method print. It worked fine. Please let me know if it is really possible to override and inherit static method.