开发者

Static method override

开发者 https://www.devze.com 2023-03-05 23:00 出处:网络
class XYZ{ public static void show(){ System.out.println(\"inside XYZ\"); } } public class StaticTest extends XYZ {
class XYZ{
    public static void show(){
        System.out.println("inside XYZ");
    }
}

public class StaticTest extends XYZ {
    public static void  show() {
        System.out.println("inside statictest");

    }

    public static void main(String args[]){
        StaticTest st =new StaticTest();
        Stati开发者_如何学GocTest.show();

    }

}

though we know static methods cant be overridden. Then what actually is happening?


Static methods belong to the class. They can't be overridden. However, if a method of the same signature as a parent class static method is defined in a child class, it hides the parent class method. StaticTest.show() is hiding the XYZ.show() method and so StaticTest.show() is the method that gets executed in the main method in the code.


Its not overriding they are two different method in two different class with same signature. but method from XYZ isn't available in child class through inheritance .

It will call method from StaticTest


It's not overriden properly said... Static methods are 'tied' to the class so

StaticTest.show();

and

XYZ.show();

are two totally different things. Note you can't invoke super.show()


To see the difference you have to use more powerful example:

class Super {
    public static void hidden(Super superObject) {
        System.out.println("Super-hidden");
        superObject.overriden();
    }

    public void overriden() {
        System.out.println("Super-overriden");
    }
}

class Sub extends Super {
    public static void hidden(Super superObject) {
        System.out.println("Sub-hidden");
        superObject.overriden();
    }

    public void overriden() {
        System.out.println("Sub-overriden");
    }
}

public class Test {
    public static void main(String[] args) {
        Super superObject = new Sub();
        superObject.hidden(superObject);
    }
}

As Samit G. already have written static methods with same signature in both base and derived classes hide the implementation and this is no-overriding. You can play a bit with the example by changing the one or the another of the static methods to non-static or changing them both to non-static to see what are the compile-errors which the java compiler rises.


It's not an override, but a separate method that hides the method in XYZ.


So as I know, any static member (method or state) is an attribute of a class, and would not be associated with any instance of a class. So in your example, XYZ is a class, and so is StaticTest (as you know). So by calling the constructor two things first happen. An Object of type Class is created. It has a member on it call showed(). Class, XYZ.class, extends from Object so has all those Object methods on it plus show(). Same with the StaticClass, the class object has show() on it as well. They both extend java.lang.Object though. An instance of StaticClass would also be an instance of XYZ. However now the more interesting question would be what happens when you call show() on st?

StaticClass st = new StaticClass();
st.show();
XYZ xyz = st;
xyz.show();

What happens there? My guess is that it is StaticClass.show() the first time and XYZ.show() the second.


Static methods are tied to classes and not instances (objects).

Hence the invocations are always ClassName.staticMethod();

When such a case of same static method in a subclass appears, its called as refining (redefining) the static method and not overriding.


// Java allows a static method to be called from an Instance/Object reference // which is not the case in other pure OOP languages like C# Dot net. // which causes this confusion. // Technically, A static method is always tied to a Class and not instance. // In other words, the binding is at compile-time for static functions. - Early Binding // // eg.

class BaseClass 
{
 public static void f1()
 {
  System.out.println("BaseClass::f1()...");
 } // End of f1().
}

public class SubClass extends BaseClass 
{
  public static void f1() 
  {
    System.out.println("SubClass::f1()...");
    // super.f1(); // non-static variable super cannot be referenced from a static context

  } // End of f1().

  public static void main(String[] args)
  {
f1();
SubClass obj1 = new SubClass();
obj1.f1();
BaseClass b1 = obj1;
b1.f1();

  } // End of main().

} // End of class.

// Output:
// SubClass::f1()...
// SubClass::f1()...
// BaseClass::f1()...

// // // So even though in this case, called with an instance b1 which is actually referring to // an object of type SuperClass, it calls the BaseClass:f1 method. //

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号