Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a super method from a static method

Tags:

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...

like image 871
opensas Avatar asked Feb 20 '11 18:02

opensas


People also ask

Can we call super in static method?

Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).

How do you call a super class static method?

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.

How do you call a super class method?

To call methods of the superclass that is overridden in the subclass. To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.

How do you call a super class method from a subclass?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.


1 Answers

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.

like image 106
Saurabh Gokhale Avatar answered Oct 10 '22 02:10

Saurabh Gokhale