0

Here if I try to override a static method without using static in the subclass it gives me an error.. while this is not a case with static variable. Why?

class A {
    static int a;
    static void a() {
       System.out.println("in A");
    }
}


class B extends A {
    int a=9;/*this does not give an error*/
    void a()/*this statement gives an error*/ {
       System.out.println("In B"+(A.a));
    }
}


class Test {

    public static void main(String []args) {   
        B b1=new B();
        b1.a();         
    }
}
MrLore
  • 3,759
  • 2
  • 28
  • 36
NeelPatwa
  • 193
  • 1
  • 11

4 Answers4

0

Fields are not over-ridable.

It has to do with Java naming scope. Java can resolve field names with much less ambiguity than it can with methods.

In B, the JVM would not know if you wanted to call A.a() or B.a()

Mark Robinson
  • 3,135
  • 1
  • 22
  • 37
0

The answer are: static applied to a method means that you can access the method without instance an object of that class, applied to a variable means that you cannot modify that variable in your code. In addiction, override doesn't care about the variable in the method you're actually overriding. This because override is replacing the method with a new one. Example in pseudo code

 Class Triangle {
     public method calcArea() {
          // generic method
      }
 }

 Class RightTriangle extend Triangle {
      public method calcArea() {
           // area needs to be calculated in different way, so I specify a new Area method
      }
 }

 Class Main {
      public Main() {
           Triangle a;
           RigthTriangle b;
           a.calcArea(); // Calling Triangle.area!
           b.calcArea(); // calling RightTriangle.area!
      }
 }               
Vargan
  • 1,277
  • 1
  • 11
  • 35
0

This is because for some strange reason, static methods can actually be invoked through a reference. The static method to invoke is based on the type of the reference rather than the object, which means that allowing an instance method with the same signature as a static method would create ambiguity as to what method to call.

For example, if this were allowed:

class A {
    static void method() {
        System.out.println("A");
    }
}

class B extends A {
    void method() {
        System.out.println("B");
    }
}

class Main {
    public static void main(String[] args) {
        A b = new B();
        b.method();
    }
}

What would happen? Should A's implementation be called because b is an A reference? Or should B's implementation be called because b is a B object?

Because both options are equally valid, allowing instance methods to "override" static methods is disallowed, ensuring all method calls are valid.


Now, this isn't true for fields (both static and nonstatic) because fields can't be overridden in subclasses, only hidden. So the compiler could easily figure out what field you want based on the type of the reference.

awksp
  • 11,764
  • 4
  • 37
  • 44
  • My question is why this thing not happen with static var?i.e static var can be override(shadowing) without using static with var.. – NeelPatwa Jun 25 '14 at 21:56
  • Read my last two sentences. They apply whether the variables are static or not – awksp Jun 25 '14 at 22:01
0

Static methods don't display run-time polymorphism. So by the rules, static methods are resolved during compile-time. By declaring a method static, you are hiding it. So no sub-classes can see it. But this is possible.

class A {
    static void test() {
        System.out.println("A");
    }
}

class B extends A {
    static void test() {
        System.out.println("B");
    }
}

This is because test() in B is B's method and test() in A is A's method which compiler understands. So if you run this class Test {

public static void main(String []args) {   
    B b1=new A();
    b1.test();         
}

At compile time seeing the reference type of b1 compiler knows that you are calling B's test even though at runtime the object would be A's

Vivin
  • 1,327
  • 2
  • 9
  • 28