Method Overriding (Programming)

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, parameter list, and return type as the superclass's overridden method. 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.

Examples

This is an example in Python. The meaning should be obvious from the syntax. 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.

 

<< PreviousWord BrowserNext >>
arcis sur aube
huang chih hsiung
the chemical wedding (album)
plummer vinson syndrome
reflected wave switching
selichot
tattooed millionaire (album)
a'gaeris (myst)
crate
balls to picasso
music martinis and misanthropy
curling ulcer
list of sports similar to baseball
cushing ulcer
alive in studio a (album)
linitis plastica
frederick weld
judas kyriakos
steve perry (musician)
scream for me brazil (album)
btc
george toparceanu
the best of bruce dickinson
bbc media village
john stanyan bigg
bad abbach
secret of cerulean sand
shelf bank
open source committee
garoowe
yorkshire cup (horse racing)
double dispatch
los patios
richard hillman
immunocompetent
types of batted balls in baseball
relche
7 a side football at the 2004 summer paralympics
list of yo yo tricks
harry glasper
luciano bottaro
the circus (erasure)
east farndon
neem oil