0

Possible Duplicate:
Can we override static method in Java?

We cannot override the static methods of the base class.

Actually I tried something like this:

// Base class
public class StaticExampleImpl {
    protected String name="overriding";
    public static void display(){
        System.out.println("static method display : base class");
    }
}

Then the derived class is as follows:

//derived class
public class StaticDemo extends StaticExampleImpl {
    // cannot override the static methods...
    //@Override
    public static void display(){
        System.out.println("child!!! static method display");
    }
    public static void main(String[] args) {
        StaticDemo d=new StaticDemo();
        d.display(); // derived class display is called rather than Base class.
    }
}

So, when I uncomment the @Override method, it gives error as "Static methods cannot be overriden". But with commenting it works fine. So, when we create the Objects and call the static methods with the instances, those work fine. so what is the difference??

Community
  • 1
  • 1
Lakshmi
  • 81
  • 2
  • 8
  • What you are trying to do simply makes no sense. Why do you want another static method with the same name as the existing one? Just make a new name. – Marko Topolnik Jul 24 '12 at 11:29

8 Answers8

5

because static methods are not get inherited.

When you uncomment @Override it means you are trying to override the static method which is not possible thats why you are getting an error.

But when you comment //@Override it means you are declaring a new method in child class.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
  • No, it gets inherited, for the above ex, comment the display of the subclass and then in the main if u call, d.display(), even it works – Lakshmi Jul 24 '12 at 11:25
  • @ lakshmi statics are NOT inherited in Java. Rather, the static members declared in a class are (subject to "access" restrictions) directly visible in the namespace of derived classes, unless they "hidden" by declarations in the derived class. – Pramod Kumar Jul 24 '12 at 11:35
1

Static methods does not belong to an instance of a class, it belongs to the actual class.

When you call d.display();, you are really calling the static method of the StaticDemo d reference's static method.

if you did :

StaticExampleImpl d2 = new StaticDemo();d2.display(), you will find that it calls the base class's display.

However, don't do this. It leads to confusing code, and is a bad way to implement inheritance poorly.

Chii
  • 14,540
  • 3
  • 37
  • 44
1

Overriding depends the an instance of a class. polymorphismis that you can subclass a class and the objects implementing those subclasses will have different behaviors for those method defined in the superclass (and overridden in the subclasses) .static methods does not belong to an instance of a class so the concept is not applicable.

amicngh
  • 7,831
  • 3
  • 35
  • 54
0

Static methods cannot be inherited. If you want to call the 'base' class static method, you have to explicitely call StaticExampleImpl.display().

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
0

Static methods are bound to class they can't be inherited thats why you can't have base class static method in derived class.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52
0

If you are trying to override a static method, there is probably something wrong with your design.

OOP and Polymorphism allows you to do the following:

public class MyClass1 {

   public String toString() { return "MyClass1 Instance"; }

}

public class MyClass2  extends MyClass1 {

   @Override
   public String toString() { return "MyClass1 Instance"; }

}

public void printSomething(MyClass1 myclass1){
      System.out.println(myclass1);
}

Inside printSomething, the toString method which is going to be called is the one on the runtime type of myClass1: when you pass inside printSomething an instance of MyClass2, its compile-type will be MyClass1 but its runtime type will be MyClass2

It is clear that to use polymorphism you need objects instances, where the actual runtime type could different from the compile type. Static methods however do not belong to any object instance, but to the class. Why don't you explain us what you are trying to achieve?

Edmondo
  • 19,559
  • 13
  • 62
  • 115
0

The following code:

StaticExampleImpl one = new StaticExampleImpl();
StaticDemo two = new StaticDemo();
StaticExampleImpl three = two;

one.display();
two.display();
three.display();

Will yield the following output:

static method display : base class
child!!! static method display
static method display : base class

As you can see, the method does not get inherited. This is why they are called 'static methods': they are called statically, not dynamically, as instance methods would be. The compile-time type of the class is what matters when calling static methods, not the runtime type.

This is also why you shouldn't call static methods through object instances. Always call them like this:

StaticExampleImpl.display();
StaticDemo.display();

This completely takes away the confusion that might (will) come up when people expect inheritance to work for these methods.

jqno
  • 15,133
  • 7
  • 57
  • 84
0

any static block in java, may be static variables, methods are loaded when the class is loaded. You probably know about class loader in java. So thing is static block (methods, variables or anything is static) is loaded once. So you can’t actually override any static block.

Commenting @Override means that you are writing another static method in sub class, but not just overriding base class method.

A N M Bazlur Rahman
  • 2,280
  • 6
  • 38
  • 51