34
votes

I know that we cannot override static methods in Java, but can someone explain the following code?

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

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

How was I able to override method a() in class B?

8
Because you didn't. B.a() is only accessible via class B. If you have something like A a = new B(); a.a();, it will print "A.a()", even though it's of type B. If it were truly overridden, then it would have printed "B.a()". Note that it is Java's odd feature that allows you to call static methods from an object instance that helps sow confusion here.dlev
What makes you think that you override a inside B? You can test that easily by adding @Override annotation before that method.Pshemo
You can test if a() is inherited by B if you remove the a() function from B. It does inherit, but it does not override. Instead it hides a() if you declare another a() function in B.Dorus
@Dorus dlev understand now. thank u.user2395360

8 Answers

53
votes

You didn't override anything here. To see for yourself, Try putting @Override annotation before public static void a() in class B and Java will throw an error.

You just defined a function in class B called a(), which is distinct (no relation whatsoever) from the function a() in class A.

But Because B.a() has the same name as a function in the parent class, it hides A.a() [As pointed by Eng. Fouad]. At runtime, the compiler uses the actual class of the declared reference to determine which method to run. For example,

B b = new B();
b.a() //prints B.a()

A a = (A)b;
a.a() //print A.a(). Uses the declared reference's class to find the method.

You cannot override static methods in Java. Remember static methods and fields are associated with the class, not with the objects. (Although, in some languages like Smalltalk, this is possible).

I found some good answers here: Why doesn't Java allow overriding of static methods?

9
votes

That's called hiding a method, as stated in the Java tutorial Overriding and Hiding Methods:

If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.

6
votes

static methods are not inherited so its B's separate copy of method

static are related to class not the state of Object

3
votes

You didn't override the method a(), because static methods are not inherited. If you had put @Override, you would have seen an error.

A.java:10: error: method does not override or implement a method from a supertype
    @Override
    ^
1 error

But that doesn't stop you from defining static methods with the same signature in both classes.

2
votes

Also, the choice of method to call depends on the declared type of the variable.

B b = null;
b.a(); // (1) prints B.a()
A a = new B();
a.a(); // (2) prints a.a() 

At (1), if the system cared about the identity of b, it would throw a NPE. and at (2), the value of a is ignored. Since a is declared as an A, A.a() is called.

1
votes

Your method is not overridden method. you just try to put @Override annotation before your method in derived class. it will give you a compile time error. so java will not allow you to override static method.

1
votes

While goblinjuice answer was accepted, I thought the example code could improved:

public class StaticTest {
    public static void main(String[] args) {
        A.print();
        B.print();
        System.out.println("-");

        A a = new A();
        B b = new B();
        a.print();
        b.print();
        System.out.println("-");

        A c = b;
        c.print();
    }
}

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

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

Produces:

A
B
-
A
B
-
A

If B had overridden print() it would have write B on the final line.

0
votes

Static methods will called by its Class name so we don't need to create class object we just cal it with class name so we can't override static

for example

class AClass{
public static void test(){

 } 
}

class BClass extends AClass{
public static void test(){}

}

class CClass extends BClass{
public static void main(String args[]){

AClass aclass=new AClass();

aclass.test(); // its wrong because static method is called 
               // by its class name it can't accept object
}
}

we just call it

AClass.test();

means static class can't be overridden if it's overridden then how to cal it .