Harshad Raut

Greenhorn
+ Follow
since Sep 23, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Harshad Raut

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.
16 years ago