Method overriding (programming)

From Wikipedia, the free encyclopedia

Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.

A subclass can give its own definition of methods which also happen to have the same signature as the method in its superclass. This means that the subclass's method has the same name and parameter list as the superclass's overridden method. Constraints on the similarity of return type vary from language to language, as some languages support covariance on return types.

Method overriding is an important feature that facilitates polymorphism in the design of object-oriented programs.

Some languages allow the programmer to prevent a method from being overridden, or disallow method overriding in certain core classes. This may or may not involve an inability to subclass from a given class.

In many cases, abstract classes are designed — i.e. classes that exist only in order to have specialized subclasses derived from them. Such abstract classes have methods that do not perform any useful operations and are meant to be overridden by specific implementations in the subclasses. Thus, the abstract superclass defines a common interface which all the subclasses inherit.

[edit] Examples

This is an example in Python. First a general class ("Person") is defined. The "self" argument refers to the instance object. The Person object can be in one of three states, and can also "talk".

   class Person:
       def __init__(self):
           self.state = 0
 
       def talk(self, sentence):
           print sentence
 
       def lie_down(self):
           self.state = 0
 
       def sit_still(self):
           self.state = 1
 
       def stand(self):
           self.state = 2

Then a "Baby" class is defined (subclassed from Person). Objects of this class cannot talk or change state, so exceptions (error conditions) are raised by all methods except "lie_down". This is done by overriding the methods "talk", "sit_still" and "stand"

   class Baby(Person):
       def talk(self, sentence):
           raise CannotSpeakError, 'This person cannot speak.'
 
       def sit_still(self):
           raise CannotSitError, 'This person cannot sit still.'
 
       def stand(self):
           raise CannotStandError, 'This person cannot stand up.'

Many more methods could be added to "Person", which can also be subclassed as "MalePerson" and "FemalePerson", for example. Subclasses of Person could then be grouped together in a data structure (a list or array), and the same methods could be called for each of them regardless of the actual class; each object would respond appropriately with its own implementation or, if it does not have one, with the implementation in the superclass.

[edit] See also

[edit] External links

In other languages