Overwrite (OOP)

from Wikipedia, the free encyclopedia

The term rewriting ( English override , literally, override 'overcome') describes a technique in object-oriented programming , which is a derived class, allows its own implementation of the base class inherited method to define.

Overriding methods is a central part of polymorphism in object orientation. Overwriting is to be distinguished from overloading .

Technical details

When overridden, the overwriting method of the derived class replaces the overridden method of the base class . The overwriting method can, however, also call the overwritten method - otherwise the overwritten methods can no longer be reached via the overwriting class.

In order for the specification of a method to be called an override, some conditions must be met. The most important conditions are:

  • The methods must exactly match in terms of the type of their parameters and the length of the parameter list.
  • The return value of the method must have the same type or a subtype of this type as the return value of the overwritten method ( covariance ).
  • The access rights of the overwriting method must not be restricted by access modifiers any more than the overridden method. However, access can be less restrictive.
  • Only instance methods can be overwritten, not class methods (static methods).
  • A method is only considered overwritten if it has actually been inherited. Methods with the same name and identical signature , but which were not inherited due to access rights, are not considered to be overwritten.