aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "overriding static methods" Watch "overriding static methods" New topic
Author

overriding static methods

Ulrich Rueth
Greenhorn

Joined: Feb 05, 2002
Posts: 3
I'm a bit confused. Though I learned during exam preparation that "static method may not be overridden" (Sybex Virtual Trainer answer), this code compiles:

class A {
static void foo() {}
}
class B extends A {
public static void foo() {}
}

Isn't this a contradiction to the statement above?
Thanks!
Ulrich
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
This question has been discussed here a zillion times. Please do a search on this forum and also look for a FAQ of this forum.
Murgan Sub
Greenhorn

Joined: Feb 06, 2002
Posts: 22
Static methods are hidden.
Instance methods are overriden
sun ram
Ranch Hand

Joined: Dec 18, 2001
Posts: 61
We can shadow the static method,
override the non-static method.
-SR


SCSecA,SCNA,SCSA,SCWCD,SCJP
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Hi Ulrich.
Please look at the following code.

When this code is executed the outcome is
StaticMethod of Test
NonStaticMethod of TestSub
Which indicates that StaticMethod was not over ridden that's why the StaticMethod of the typecast object type was invoked but since NonStaticMethod was overridden, although we typecast it to be of type Test it still invoked NonStaticMethod of TestSub, which is the original object and not the typecasted one.
Hope this helps.


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: overriding static methods
 
Similar Threads
Ambiguious Questions...
Constructor
Polymorphism and Static methods
Objective 7.6 Java Operators (instanceof) Operator
Inheritance Question