Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can static method overridden?

 
derek_ho
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this true:
"static methods can be overridden by static methods only".( from java_cert exam)
The answer is false. can someone tell me why?
can static method be overriden? If yes, what are ways of overriding a static method?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by derek_ho:
Is this true:
"static methods can be overridden by static methods only".( from java_cert exam)
The answer is false. can someone tell me why?
can static method be overriden? If yes, what are ways of overriding a static method?

No, it is not true. Static methods cannot be overridden, rather, they can be hidden.
This program shows the difference between method hiding and overridding.

As you can see, line 1 works as expected. You are making a method call on an SuperClass object reference variable, but because the actual instance is of type SubClass, the overridden method gets called.
In line 2, when we attempt to do the same thing with a static method, we see that the same rules do not apply. The static method that gets invoked is the one that is a member of the reference variable's class, NOT the one that is a member of the actual object instance!
This shows that there is a very real, tangible, important distinction between static and instance methods, and between hiding and overridding. It also shows why it is Bad Form to refer to a static member by an reference variable; you should always use the class name to access a static member. In the above line 2, if you had written
SubClass.staticPrintClassName();
SuperClass.staticPrintClassName();
it would be clear which method would be invoked.

Rob
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also the following thread for a good discussion about the subject:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014224
HIH
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,Based on the code,how to invoke the method printClassName() of superClass?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seany,
there are only two ways.
One, instantiate an object of type SuperClass and call that method.
SuperClass sc = new SuperClass();
sc.printClassName();
Two, create a subclass of SuperClass. Subclasses can call super.printClassName() and access the overridden method.

Rob
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two, create a subclass of SuperClass. Subclasses can call super.printClassName() and access the overridden method

Rob, thank you.
I know the first way you told.
But as for the second,It's only be called in subclass? If out the subClass, can it?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Seany. If you have an object instance of type SubClass, and you call printClassName()on that object, there is no way for you to invoke the SuperClass version. The only method available to you is the overridden version.
Rob
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see,thank you very much,Rob!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic