There are the following lines written in Java from the Android SDK :

 public class MainActivity extends AppCompatActivity { @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } } 

In this example, the MainActivity class inherits the AppCompatActivity class and overrides the onRestoreInstanceState method to which it receives an object of type Bundle . This instance of the savedInstanceState can contain some kind of data, i.e. he is not null .

The question is: how are such things implemented, in the sense that an argument that already contains something in itself gets into the overdetermined method? Or, if we ignore Android and take pure Java - can an ancestor class pass any arguments to the overridden method in the heir class?

  • those. in the class from which the method that was overridden is inherited, I understand correctly? - Artem Konovalov
  • Yes, and pass arguments to it. As far as I understand that in the ancestor class, this method should be abstract, but in the source code I already see a concrete implementation, and I can’t find out again which of the higher classes in the hierarchy calls this method - Kirill Stoianov
  • 2
    You cannot call a redefined method from a parent class unless it is abstract. In your case, the method is called on the descendant class, and in this method, the method in the class that is higher in the hierarchy is already twitching. - Artem Konovalov

3 answers 3

These are not overdetermined superclass methods, but callbacks (callback methods) that are implemented through the callback interface and are specifically made to send events between unrelated classes.

Inheritance from the Activity class in this case is not the source of the value in the onRestoreInstanceState() method - the value there appears on the create / re-create activation event and is generated by the system, transmitted via the interface.

It is considered to be good practice to distinguish the callback methods with the preposition on- (onCreate, onResume ...) - on this pretext we see that there is a callback and not inheritance.

  • so far I have found in the source code only the protected void onCreate (@Nullable Bundle savedInstanceState) method of the Activity class. What is the interface for the onCreate call? - Kirill Stoianov
  • one
    @KirillStoianov Documentation is not particularly spread on this subject, putting before the fact that life-cycle methods are controlled by callbacks that the system itself generates, but I’m not happy to understand that. It's safe to say that the Activity class is deeply integrated into the system and simply accept that these callbacks react to system events. Do you need it for a specific purpose or out of mere curiosity? The fact is that the answer to your question is no, the parent does not, and cannot, pass arguments to the child, another mechanism works here that uses system functions. - pavlofff
  • I understood, in general, everything is done by the system, and something gets into this method only if someone transfers something there? I just thought that there are some Java mechanisms that I did not know about! - Kirill Stoianov
  • one
    @KirillStoianov This mechanism is a normal callback interface (like widget clicker listeners, you override the onClick() method and it gets the View argument you clicked on), only the event generates the event, not the user code, but . - pavlofff

You cannot call a redefined method from a parent class unless it is abstract. In your case, the method is called on the descendant class, and in this method, the method in the class that is higher in the hierarchy is already twitching.

    In java, and in other languages, this is called inheritance. When you inherit one class from another, and then create an instance of the descendant class, it is also an instance of the parent class, i.e. if you enter the parent variable to assign an instance of the descendant class Parent parent = new Child() , then this will not be an error. And if you call the method that is on the parent, then the search will be performed first for the presence of a method with the same signature as that called by the parent, i.e. method that overrides the parent method (Override). At the same time, adding the @Override annotation is not necessary (as in my example), but it is a good form. I advise you to read some information about inheritance and the PLO as a whole. Good luck!

     class Parent { void passArg(String someArg) { System.out.println("Вызов в классе-родителе: " + someArg); } } class Child extends Parent { void passArg(String someArg) { super.passArg(someArg); System.out.println("Вызов в классе-потомке: " + someArg); } } // Где-то в функции main Parent child = new Child(); child.passArg("Я родился!"); // Вывод будет таким: // Вызов в классе-родителе: Я родился! // Вызов в классе-потомке: Я родился! Parent parent = new Parent(); parent.passArg("Я родился"); // Вывод будет таким: // Вызов в классе-родителе: Я родился 

    PS In the example above, the parent function works only because I explicitly call it in the overridden function on the super.passArg(someArg) via super.passArg(someArg) , passing the already existing argument there.