What is and what is an override (method override) for in programming?

Asked

Viewed 333 times

0

The title of the question basically says it all: what is and what is a Override (method envelope) in the programming?

Show 3 more comments

2 answers

2


The method replacement , in the programming object-oriented , is a language resource that allows a subclass or child class provides a specific implementation of a method that is already provided by one of its superclasses or parent classes.

The implementation in the subclass replaces the implementation in the superclass, providing a method that has the same name, the same parameters or signature and the same type of return as the method in parent class.

The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, the version in parent class will be executed, but if an object of the subclass is used to invoke the method, the version in child class will be executed.

Some languages allow a programmer to prevent the replacement of a method.

The main head start method substitution is that the class can give its own specific implementation to an inherited method without modifying the code of the parent class .

  • I gave +1 but it is not entirely correct. The superscript may be an abstract method, in this case there is no implementation in the parent class.

  • Thank you, this study was made minutes before this post, but thank you for the information!

1

Imagine you have a class groundwork calling for Person, with various attributes that every person must necessarily contain, such as rank, age, name, and a method called Salary().

All people need to have their salary paid through this method, but we know that wages for each person differ greatly, depending on the position, region and various other factors.

We can then, overwrite the method Salary() which has been inherited from the base class Person, and change the logic of it. Note that we are still inheriting this method, keeping its parameters, its name, and its return type (if any), but we can specify how this function should behave in the class to which we override the method.

The method in the class groundwork will not be changed, and all other classes that inherit from it will still have this method and may also overwrite the same if need be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.