calling a super method from a static method

opensas :

Is it possible to call a super static method from child static method?

I mean, in a generic way, so far now I have the following:

public class BaseController extends Controller {
    static void init() {
        //init stuff
    }
}

public class ChildController extends BaseController {
    static void init() {
        BaseController.loadState();
        // more init stuff
    }

}

and it works, but I'd like to do it in a generic way, something like calling super.loadState(), which doesn't seem to work...

sgokhales :

In Java, static methods cannot be overidden. The reason is neatly explained here

So, it doesn't depend on the object that it is being referenced. But instead, it depends on the type of reference. Hence, static method is said to hide another static method and not override it.

For example (Cat is a subclass of Animal):

public class Animal {
    public static void hide() {
        System.out.format("The hide method in Animal.%n");
    }
    public void override() {
        System.out.format("The override method in Animal.%n");
    }
}

public class Cat extends Animal {
    public static void hide() {
        System.out.format("The hide method in Cat.%n");
    }
    public void override() {
        System.out.format("The override method in Cat.%n");
    }
}

Main class:

public static void main(String[] args) {
    Cat myCat = new Cat();
    System.out.println("Create a Cat instance ...");
    myCat.hide(); 
    Cat.hide();
    myCat.override();  

    Animal myAnimal = myCat;
    System.out.println("\nCast the Cat instance to Animal...");
    Animal.hide();     
    myAnimal.override();

    Animal myAnimal1 = new Animal();
    System.out.println("\nCreate an Animal instance....");
    Animal.hide();     
    myAnimal.override();
}

Now, the output would be as given below

Create a Cat instance ...
The hide method in Cat.
The hide method in Cat.
The override method in Cat.  

Cast the Cat instance to Animal...
The hide method in Animal.
The override method in Cat.

Create an Animal instance....
The hide method in Animal.
The override method in Animal.

For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called.

In other words, call to static methods are mapped at the compile time and depends on the declared type of the reference (Parent in this case) and not the instance the reference points at runtime. In the example, the compile-time type of myAnimal is Animal. Thus, the runtime system invokes the hide method defined in Animal.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling super static method from designated initializer

From Dev

Calling super method from interface

From Java

Calling super method from within super class

From Dev

Calling a non static method from a in a static context

From Dev

Typescript `super` calling from overridden method

From Dev

Calling an Ember _super method from Promise handler

From Java

Calling super super class method

From Dev

Calling super class' async method from async method in extending class

From Javascript

Calling an overridden static method from parent

From Dev

Calling a static void Java method from JNI

From Dev

Calling a Java static method with generics from Scala

From Java

Calling Java Main method from static block

From Dev

calling static method from inside the class

From Dev

Calling a static method from NSTimer. Is it possible?

From Dev

Calling a static template method from a template function

From Dev

Calling static method from inside a task

From Dev

Calling a static method from a generic constraint Dart

From Dev

Calling a static method from cshtml file

From Dev

Calling non static method from static method USING instance created in the static method

From Java

calling static method in java

From Java

Calling the instance of a static method

From Java

Calling static method in python

From Java

Calling static method on a class?

From Dev

Is calling a static method from an instance method thread safe?

From Java

Python super method and calling alternatives

From Java

java calling method before super

From Dev

Calling non static method in static method

From Dev

Calling a static method from a non-static context

From Dev

Calling super().method() in a subclass that does not override method

Related Related

HotTag

Archive