Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can Static Methods be overridden ?

843789May 11 2009 — edited May 11 2009
My question is can static methods be overridden ?

I found this thread [Can Static Method be overridden ?|http://forums.sun.com/thread.jspa?threadID=644752&start=0&tstart=0] .Since it was too old,i have created this new thread.



According to that thread discussion, Java language states that static methods cannot be overridden.

To which kajbj posted a program which did allow overriding of static methods.To quote him :
I filed a bug report on it. I don't know if it's expected behaviour or not, but I expect the compiler to complain if you add @Override to a static method (since it can't be overridden)

/Kaj 
This is one small program code which i wrote ,which did not allow static methods to be overridden,but no error as such was displayed.
package fundamentals;

class SuperClass
{
  
  public static String getName()
  {
	  return "HI,CLASS...SUPER CLASS";
  }
  
  public int getAge()
  {
	  return 20;
  }

} // END OF SUPER CLASS

public class SubClass extends SuperClass{

public static void main(String[] args)
 {

		SubClass objSubClass=new SubClass();
		SuperClass objSuperClass=new SubClass();
		
		System.out.println(objSubClass.getName());      // SUB CLASS 
		System.out.println(objSuperClass.getName());   // SUPER CLASS

		System.out.println(objSubClass.getAge());        // SUB CLASS 
		System.out.println(objSuperClass.getAge());     // SUPER CLASS

}

                 public    static String getName()
                 {
                   return "HI,CLASS...SUB CLASS";
                 }

                 public int getAge()
                  {
                    return 40;
                  }

} // END OF MAIN CLASS
Which gives the O/P :
HI,CLASS...SUB CLASS
HI,CLASS...SUPER CLASS
40
40
So,the static method was not overridden.

But ,why was no error message displayed ?


Also when i modify the subclass static method,by removing the static keyword as follows :
public  String getName()
                 {
                   return "HI,CLASS...SUB CLASS";
                 }
A Error Message as :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	This instance method cannot override the static method from SuperClass
is displayed.

Why this message is displayed after i remove the static keyword ?


So can we say that Java does not allow static method to be overridden but does not display a compile/run time error when this is done ?

Is this a bug as stated by kajbj ?

Please let me know if i am not clear.If this question has been answered somewhere else in this forum,kindly let me know.

Thank you for your consideration.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2009
Added on May 11 2009
7 comments
556 views