(Redirected from Object orientation)

HomePage | Recent changes | View source | Discuss this page | Page history | Log in |

Printable version | Privacy policy

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:

  • Abstraction mandates that each class provide to the rest of the program a defined interface -- thus allowing the programmer who uses the class to not know or think about the internal workings of the class.
  • Encapsulation mandates that a class have both data, and the functions that operate on that data, and that no other code is allowed to access that data directly. This is a major point of debate; some object-oriented languages provide it, and others don't.
  • Inheritance allows a class to inherit from a parent class, or superclass. An instance of the child class, or subclass then has access to all the functions and data of the parent class, as well as its own additions. Some languages support multiple inheritance, in which a class can inherit from multiple parent classes.
  • Polymorphism has two aspects: polymorphic variables and polymorphic operations. A polymorphic variable is one that can contain objects of different types at different times (for example, instances of different classes); a polymorphic operation is one that executes different code depending on what types of objects it is applied to. Polymorphic method calls (called "virtual function calls" in C++) are the variety of polymorphism that's unique to object-oriented languages; by default, calling a method foo on an object of class Bar will run the same code as calling foo on an object of Bar's superclass, but if Bar defines its own foo method (overriding its superclass's method), Bar's foo method will be run instead.

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" )

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" )

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.