Method overriding

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

The overriding methods is a central component of polymorphism in object orientation. Overwriting can be distinguished from overcharging.

Technical details

When overridden, the overriding method of the derived class replaces the overridden method of the base class. However, the overriding method can also call the overridden method - otherwise the overridden methods on the overriding class can not be reached.

Thus, the specification of a method can be called overriding, some conditions must be met. The main conditions are:

  • The methods must match exactly the type of their parameters and in the length of the parameter list.
  • The return value of the method must have the same type or a subtype of this type have as the return value of the overridden method ( covariance ).
  • The overriding method must not be limited in the access rights as the overridden method through access modifiers. Access may, however, be less restrictive.
  • It can only be overridden instance methods, not class methods (static methods).
  • One method applies only when overwritten if it was actually inherited. Methods with the same name and with the same signature, but which were not inherited because of access rights shall not be deemed overwritten.
  • Object-Oriented Programming
789483
de