Programming Concepts: Object-oriented programming (OOP)

From Wikibooks, open books for an open world
Jump to: navigation, search

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Programming paradigms Object-oriented programming Elements of Object-Oriented Programming →


Overview[edit]

Where Procedure-oriented programming uses procedures to make code easier to write and understand, Object-oriented programming (OOP) goes a step further and uses objects to make code easier to create and work with.

In OOP, the word "object" has special meaning: objects are defined as a specific way of organizing source code. Briefly, an Object is the combination of:

  • Some data (i.e. variables) that are related to each other
  • Procedures specifically designed to work with that data
Examples

A car could be represented by a Car object.

The data might include: colour, manufacturer, top-speed

Procedures might include: start_engine, accelerate, break

Background[edit]

If a program only requires a few lines of source code to write down, there is no advantage to using this technique. Procedure-oriented and Object-oriented programming were invented because programs were getting longer and longer, and were difficult to work with. Programmers needed more structure to simplify the programming process.

A program of moderate size and complexity can be simplified using procedures. With especially large or complex programs, procedures are not enough; OOP became popular as a way of handling these very complex programs.

A program may be complex in the raw source code - many lines of code, many procedures. Or it may be complex in the way it's written - many human authors, many interactions between different parts. OOP helps with both.

We will look at the detailed reasons for and against once we've covered the key concepts.

Improvements to Procedure-Oriented Programming[edit]

OOP started from observing ways of writing Procedure-oriented programs that were more effective (easier to write, had fewer bugs) than others. A few techniques that were optional in Procedure-oriented programming became the core of OOP:

  • Encapsulation
  • Aggregation
  • Composition
  • Interfaces

TODO: add SIMPLE definitions of each of the above; they are covered in more detail later in the section on OOP Design Principles