T
T
Tsuzukeru2021-06-21 10:22:41
Java
Tsuzukeru, 2021-06-21 10:22:41

When should super be called in an overridden method?

When overriding any method in a class heir, IDEA automatically prescribes a call to the parent method.

override fun method() {
        super.method()
    }


For example, in the callback methods of the Android system, these calls must be present. Why dont know. But in all examples they are present.
But if you create a thread using Thread or Runnable and override run(), then you can remove super.run() inside it and this will not change anything. How to understand when this call is needed and when not?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
acwartz, 2021-06-21
@Tsuzukeru

It is very dependent on the ancestor class, and the entire object model of the programming language. Suddenly, all classes are registered somewhere and calling super is just a given?
If it does work in the constructor that needs to be done so that you can work with it, then a call is needed.
For example, the ancestor class initializes some sockets or something else, and you decide to simply add your own property,
without super the class will not work as it should.
Moreover, you yourself decide when to do it, at least not to do it, at least at the beginning, at least at the end, at least by condition.

For example, in the callback methods of the Android system, these calls must be present. Why dont know.

Because the system sees that your code is defined by you, and delegates the system code call to it. If you are replacing system code (hidden in supers) - do not call super (which is generally rare, because it is wrapped in an exceptional construct so as not to break the UI and let at least system code work, in case of errors in your program), if you do not replace and add, do.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question