• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding static mtds

 
Vidya Krishnamurthi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian
Brian, according to me it's 100% safe and yes they are masked or shadowed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic