[Home]Object-oriented

HomePage | Recent Changes | Preferences

An object-oriented programming language (as opposed to a procedural or functional programming language) encourages the programmer to think of her program in terms of primarily the data and the operations on that data. Procedural languages encourage the programmer to think in terms of functions that get passed variables. To put it another way, a procedural programmer manipulated data by calling functions on it. An object-oriented programmer sends messages to data objects telling them to call functions on themselves.

There are often said to be four major aspects to object orientation: abstraction, encapsulation, inheritance, and polymorphism. These will now be (loosely) summarized:

For a simplistic example, let's say you wanted to program a computer to open a door when the user types "open" and close the door when the user types anything else.

In a procedural language you might write:

   function main()
   {
      a = getinput();
      if( a == "open" )
         open(door);
      else
         close(door);
   }

In a object oriented programming language you might do the following:

   class door
   {
      function open();
      function close();
      function process_request(user a)
      {
         if( a.request() == "open" )
            self.open();
         else
            self.close();
      }
   }

Why is this useful? A typical procedural language needs different functions for each thing that must be closed. In a object-oriented language, a door, box, cabinet, etc may all provide the same interface, saving the programmer a lot of memorization.

Languages allowing object orientation include:

Those languages marked "purely object-oriented" do not include any procedural features.

Additionally, some languages include abstract data type support, but not all of the features of object orientation. PHP 4.x, for example, includes no provisions for inheritance or polymorphism, but does allow for a concept of "class", and thus enables the programmer to use unenforced versions of abstraction and encapsulation. Sometimes this is referred to as an object-based language. This is often useful -- inheritance and polymorphism are usually used to reduce [code bloat]?, but abstraction and encapsulation are used to increase code clarity, quite independent of the other two.

/Talk


HomePage | Recent Changes | Preferences
This page is read-only | View other revisions
Last edited December 19, 2001 7:47 am by Taw (diff)
Search: