Science Fair Projects Ideas - Method overriding (programming)

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

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.

Last updated: 05-12-2005 19:33:32
10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice