wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding static mtds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding static mtds" Watch "overriding static mtds" New topic
Author

overriding static mtds

Vidya Krishnamurthi
Greenhorn

Joined: Jan 12, 2003
Posts: 18
Dear All
class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
public static void main(String[] args) {
C c = new C();
c.m();
B b = c;
b.m();
A a = b;
a.m();
}
}
following is one of qns from dan's mock tests.
i think this shud give a compiler err ..
static mtds cannot be overidden thats what i thot.
could any 1 throw some light on this issue
thanks
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Vidya
I would say that it would be better to say that static methods wouldn't be overridden. That is what is being done in this code is that the method is being redeclared. So the inherited static method is being replaced by the method declared in the new class. That is when class B also declares the same method as class A then class B doesn't overrides class A's method but provides a new method. Same for class C. What has been done is syntactically correct but is not overriding.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
So it is safe to say that static methods are never overridden. Instead, you say they are "masked" or "shadowed", correct?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Brian
Brian, according to me it's 100% safe and yes they are masked or shadowed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding static mtds