File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding static mtds" Watch "overriding static mtds" New topic

overriding static mtds

Vidya Krishnamurthi

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();
B b = c;
A a = b;
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
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
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: 1090
Hi Brian
Brian, according to me it's 100% safe and yes they are masked or shadowed.
I agree. Here's the link:
subject: overriding static mtds
It's not a secret anymore!