Your continued donations keep Wikipedia running!    

Object-oriented programming

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer science, object-oriented programming, OOP for short, is a computer programming paradigm.

The idea behind object-oriented programming is that a computer program may be seen as composed of a collection of individual units, or objects, that act on each other, as opposed to a traditional view in which a program may be seen as a collection of functions or procedures, or simply as a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects.

Object-oriented programming is claimed to give more flexibility, easing changes to programs, and is widely popular in large scale software engineering. Furthermore, proponents of OOP claim that OOP is easier to learn for those new to computer programming than previous approaches, and that the OOP approach is often simpler to develop and to maintain, lending itself to more direct analysis, coding, and understanding of complex situations and procedures than other programming methods. Translation from real-world phenomena/objects (and conversely) is eased because there is (generally a many-to-one) direct mapping from the real-world to the object-oriented program. The converse mapping, from the object-oriented program back to the real-world, is by way of a one-to-many mapping, where each 'many' is seen to be a collection of well-defined subsets, which together compose a real-world object. The corresponding object defined in the program may be seen as representing some proper subset of the real-world object's features.

Contents

Fundamental concepts

Object-oriented programming (OOP) emphasizes the following concepts:

  • Class — the unit of definition of data and behavior (functionality) for some kind-of-thing, a class (for example, Dog) is the basis of modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, and the code for a class should be coherent and decoupled (as should the code for any good pre-OOP function). With such modularity, the structure of a program will correspond to the aspects of the problem that the program is intended to solve.
  • Object — an instance of a class, an object (for example, "Rin Tin Tin" the Dog) is the run-time manifestation (instantiation) of a particular exemplar of a class. Each object has its own data, though the code within a class is shared for economy. (Thus, OOLs must be capable of being reentrant.)
  • Encapsulation — a type of privacy applied to the data and some of the methods (that is, functions or subroutines) in a class, encapsulation ensures that an object can be changed only through established channels (namely, the class's public methods). Encapsulation means wrapping up of data and associated functions into a single unit(called class).Each object exposes an interface — those public methods, which specify how other objects may read or modify it. An interface can prevent, for example, any caller from adding a list of children to a Dog when the Dog is less than one year old.
  • Inheritance — a mechanism for creating subclasses, inheritance provides a way to define a (sub)class as a specialization or subtype or extension of a more general class (as Dog is a subclass of Canidae); a subclass acquires all the data and methods of all of its superclasses, but it can add or change data or methods as the programmer chooses. Inheritance is the "is-a" relationship: a Dog is-a Canidae. This is in contrast to composition, the "has-a" relationship, which user-defined datatypes brought to computer science: a Dog has-a mother (another Dog) and has-a father, etc.
  • Abstraction — the ability of a program to ignore the details of an object's (sub)class and work at a more generic level when appropriate; For example, "Rin Tin Tin" the Dog may be treated as a Dog much of the time, but when appropriate he is abstracted to the level of Canidae (superclass of Dog) or Carnivora (superclass of Canidae), and so on.
  • Polymorphism — polymorphism is behavior that varies depending on the class in which the behavior is invoked. For example, the result of bark() for a Dog would differ from the result of bark() for a Jackal; and in a more sophisticated animal-emulation program, bark() would differ for a Chihuahua and a Saint Bernard.

Polymorphism shows the advantage of object orientation most clearly: given a list of animals in a Zoo, raiseRuckus() can loop through the list and ask each animal to bark() — and each animal will bark() appropriately, without raiseRuckus() needing to examine the subclass of each animal.

An object-based language is a language that has most of the properties of an object-oriented language, but lacks inheritance.

OOP as a new paradigm, point of view or marketing term

OOP is subject to much contention as to its precise definition or its principal ideas.

In the most general terms, OOP is the practice of writing program text decomposed in modules that encapsulate the representation of one data type per module, instead of collections of functions that call each other, or clauses that trigger each other.

They have been brought together, with associated terminology, to create a programming framework. Together the ideas behind OO are said to be so powerful they create a paradigm shift in programming.

Other paradigms such as functional and procedural programming focus primarily on the actions, and logical programming focuses on the logical assertions that trigger execution of program code.

OOP arose independently out of research into simulation system oriented languages, with SIMULA 67, and out of research into highly secure system architectures, with capability based OS and CPU architectures.

Some experts say that the original definition of Object-oriented came from the object in grammar. The requirements for the software are always subject-oriented and since the requirements for the subject are often complicated, monolithic complicated programs were created.

Some of the researchers at that time started thinking in an object-oriented way, which was a paradigm shift from the usual subject-oriented thinking.

According to the object-oriented principles, the verb is attached to the object and logic associated to the requirement is handled in the object. The following are some examples of the ways by which a subject oriented requirement is translated into object oriented thinking:

  • Subject oriented: The Sales Application saves the Transaction
  • Object oriented: The Transaction saves itself upon receiving a message from the Sales Application
  • Subject oriented: The Sales Application prints the Receipt
  • Object oriented: The Receipt prints itself upon receiving a message from the Sales Application

One distinguishing feature of OOP is the handling of subtypes of data types.

Objects' data are generally required to satisfy programmer-defined constraints (i.e. class invariants). A datatype restricted with such constraint is a subtype of the same datatype without the constraint. These constraints are then both relied on and preserved by the actions (methods) defined for the data. These constraints may either be explicitly declared or implicitly assumed by the programmer. Object-oriented languages provide mechanisms for ensuring that such assumptions are local to one part of the program. They are usually part of documentation of object-oriented programs.

OOP itself has been used to market many products and services and the actual definitions and benefits attributed to OOP have often been colored by commercial marketing goals. Similarly, many programming languages have a specific view to OOP that is less general in certain aspects from the more general definition.

Widely-used terminology distinguishes object-oriented programming from object-based. The former is held to include inheritance (described below), while the latter does not.

The exact definitions of these have some variation depending on point of view. In particular, languages with static typing often have slightly different views of OO than languages with dynamic typing, caused by focus on compile-time vs. run-time properties of the programs.

Notes: Abstraction is important to but not unique to OOP.

Reusability is the benefit most often claimed for OOP. However, that is sometimes disputed as being a large or primary benefit. The ability to maintain a program once written, the ability to do localized debugging, and the ability to do much larger parallel development efforts are all cited as more significant reasons to use an OOP language.

OOP is often called a paradigm rather than a style or type of programming to emphasize the point that OOP can change the way software is developed, by changing the way that programmers and software engineers think about software.

The paradigm of OOP is essentially not that of programming but one of design. A system is designed by defining the objects that will exist in that system, the code which actually does the work is irrelevant to the object, or the people using the object, due to encapsulation. The challenge in OOP therefore is of designing a sane object system.

It should be noted that there are distinct parallels between the object-oriented paradigm and Systems theory. OOP focuses on objects as units in a system, whereas systems theory focuses on the system itself. In between, one may find software design patterns or other techniques that use classes and objects as building blocks for larger components. Such components can be seen as an intermediate step from the object-oriented paradigm towards the more "real-life oriented" models of systems theory.

Actor model

OOP is a decomposition paradigm for program code, not a model for computation.

OOP is often confused with the Actor model of computation. In response to a message that it receives, an Actor can make local decisions, create more Actors, send more messages, and determine how to respond to the next message received.

Almost all OOP languages and systems, including all the major ones such as SIMULA, Smalltalk, C++, Java, Python and C# have message passing programming capabilities.

See Actor model implementations for a discussion on implementations of the Actor model.

In OOP the emphasis is not on how computation is organized, but on how program text is decomposed into modules, because it is this decomposition that matters as to the program text's comprehensibility and maintainability.

OOP is based on the assumption that the program text's comprehensibility and maintainability is improved by decomposing it into modules, and that the best way to decompose it into modules is to minimize dependencies among modules and the cohesion of functions inside the module, and that this is best achieved by encapsulating the representation of a data type in each module.

Subparadigms

There are several distinct styles of object-oriented programming. The distinctions between different styles occur because different programming languages emphasize different aspects of object-oriented facilities and combine with other constructs in different ways.

OOP with procedural languages

In procedural languages, OOP often appears as a form where data types are extended to behave like a type of an object in OOP, very similar to an abstract data type with an extension such as inheritance. Each method is actually a subprogram which is syntactically bound to a class.

Static typing with the object-oriented paradigm

Many object-oriented programming languages, such as C++ and Java, have a static type system that can be used to check and enforce constraints of object-oriented design to some extent at compile-time, i.e. statically. Object-oriented facilities combine with static typing in various ways. Classes are types of objects. Many object-oriented languages provide mechanisms for statically checking the type of method parameters, types of private and public data members, types of object references and check the correctness of inheritance and subtyping relationships. Static type checking can also check API compatibility, enforce data constraints on the users of libraries created with object-oriented methods and reduce the number of type checks performed at run-time for various forms of method dispatch.

Some object-oriented languages, such as Eiffel, supplement the type system with assertions specifying and documenting invariants of classes and the contracts of methods, though current Eiffel compilers only check these at run-time, i.e. dynamically.

See Class-based OOP.

Prototype-based model

Other than using classes, prototyping is another, less popular, means of achieving object-oriented behavior sharing. After an object is defined, another similar object will be defined by referring to the original one as a template, then listing the new object's differences from the original. Perhaps the most popular prototype-based language is JavaScript, which is an implementation of ECMAScript; Self, a programming language developed by Sun Microsystems is another instance of a language that uses prototyping for behavior sharing rather than classification. NewtonScript, Act1, Io and DELEGATION are other examples. Hybrid and Exemplars use both prototyping and classification. In prototyping systems, objects themselves are the templates, while classification systems use classes as templates for objects.

The classification approach is so predominant in OOP that many people would define objects as encapsulations that share data by classification and inheritance. However, the more generic term "behavior sharing" acknowledges alternate techniques such as prototyping.

See Prototype-based programming.

Object-based model

Object-based programming is centered around the creation of objects and their interactions, but may not have some of the key features of the class-based object-oriented paradigm such as inheritance. Such object-based systems are usually not regarded as object-oriented, because inheritance (viewing delegation as a form of inheritance) is typically identified as the core feature of OOP.

Multimethod model

In this model, the "receiver" argument to a message is not given special status in message dispatch. Instead, the runtime values of all arguments to message are consulted to determine which method should be executed at runtime. This is related to double or multimethod dispatch.

Note that some feel that set theory or predicate logic is better suited to tackle this kind of complexity.

Possible programming mistakes

There are several common mistakes which programmers can commit in object oriented programming. For example, checking the type of an object instead of its membership is a common antipattern that counteracts the benefits of inheritance and polymorphism.

Critique

Hierarchical taxonomies often do not match the real world and real-world changes according to some critics, and should be avoided. However, many OOP proponents also suggest avoiding hierarchies and instead using OO techniques such as "composition". A simple way of avoiding over-specification of hierarchies when modeling the real world is to consider the most specific types of objects and model relationships between those.

Also, many feel that OOP runs counter to the philosophy of relational modeling and relational databases, returning to the navigational database arrangements of the 1960s. It is not clear that this is the fault of OOP, since database modeling is based fundamentally on different premises than object-oriented modeling. In any case, relational database tables map to associations in object-oriented models, and the differences seem to be purely due to differences in focus. There is a history of misinterpretation of the relationship between object-oriented and relational modeling, which may muddy this issue. Also, there are variances in opinions about the roles and definitions of each. For example, some feel that OOP unnecessarily will copy noun relationship information from the database, when "once and only once" (no duplication) mantra dictates that such is bad practice. Others, in contrast, feel that OOP does not require this duplication, even though some existing OOP-to-relational database products mistakenly take this view, confusing object's data with relationship data. These people would also argue that strict distinctions should be made between data associated with the modelled objects, data associated with the roles and data associated with associations; in particular, object's data should not be (directly) stored in databases by this view, because databases are not a suitable storage for objects, the object already has some mechanism for storing its private information, and storage in database would require unnecessary replication between the object's image in its own storage and the database. The impedance mismatch between databases and OOP is caused by difference of scale between operations performed by objects and databases; database transactions, the smallest unit of work performed by databases, are much larger than any operations provided by OOP objects. Instead, by this view, databases are good for storing relationships between objects and the references to objects that are associated with roles that those relationships are built on; objects' data could only be stored in databases after collecting and summarising data from groups of objects. Object's private representation details have no place in databases.

Needless to say, the "proper" relationship between OOP and databases is a complex and contentious topic which currently has no consensus solution.

While it is claimed that OOP is better for "large applications", others feel that large applications should instead be reduced to many small applications, such as event-driven procedures that "feed" off of a database and declarative programming-based user interface frameworks.

The bottom line of the conflict seems to be that OOP is mostly a behaviorist view of software design which conflicts with the data-centric, declarative view. In the first, the "interfaces" are primarily behaviors, and data is grouped into objects. In the second the interfaces are primarily data (declarations) and behaviours are grouped into functions, such as "tasks", or "events". The tradeoffs of each approach are complex and often delve deep into human psychology theories. Sometimes both are used, such that OOP is used to build platform facilities and functional or declarative method is used to build applications for the platform.

Some feel that past criticisms leveled against procedural techniques are based upon poor languages, poor coding practices, or lack of knowledge about how to properly use databases instead of code to manage state and "noun models".

It is recognized that OOP does not necessarily mean lack of complexity. Meta class programming for example is a demanding skill, and OOP programs can have a complex web of shared or distinct responsibilities, attributes and methods. It can be challenging to distribute responsibility over objects, or classes—one of many popular implementation schemes.

Formal definition

There have been several attempts on formalizing the concepts used in object-oriented programming. The following concepts and constructs have been used as interpretations of OOP concepts:


Attempts to find a consensus definition or theory behind objects have not proven very successful, and often diverge widely. For example, some definitions focus on mental activities, and some on mere program structuring. One of the simpler definitions is that OOP is the act of using "map" data structures or arrays that can contain functions and pointers to other maps, all with some syntactic and scoping sugar on top. Inheritance can be performed by cloning the maps (sometimes called "prototyping").

OOP in scripting

In recent years, object-based programming has become especially popular in scripting programming languages, with abstraction, encapsulation, reusability, and ease of use being the most commonly cited reasons, (the value of inheritance in these languages is often questioned). Smalltalk is probably the first language that fits into this category. Python and Ruby are relatively recent languages that were built from the ground up with OOP in mind, while the popular Perl and PHP scripting languages have been slowly adding new object oriented features since versions 5 and 4, respectively. The ability of objects to represent "real world" entities is one reason for the popularity of JavaScript and ECMAScript, which is argued to be well suited to representing the Document Object Model of HTML and XML documents on the Internet.

See also: dynamic language

History

The concept of objects and instances in computing had its first major breakthrough with the PDP-1 system at MIT which was probably the earliest example of capability based architecture. Another early example was Sketchpad made by Ivan Sutherland in 1963; however, this was an application and not a programming paradigm.

Objects as programming entities were first introduced in Simula 67, a programming language designed for making simulations, created by Ole-Johan Dahl and Kristen Nygaard of the Norwegian Computing Centre in Oslo. (Reportedly, the story is that they were working on ship simulations, and were confounded by the combinatorial explosion of how the different attributes from different ships could affect one another. The idea occurred to group the different types of ships into different classes of objects, each class of objects being responsible for defining its own data and behavior.) Such an approach was a simple extrapolation of concepts earlier used in analog programming. On analog computers, such direct mapping from real-world phenomena/objects to analog phenomena/objects (and conversely), was (and is) called 'simulation.' Simula not only introduced the notion of classes, but also of instances of classes, which is probably the first explicit use of those notions.

The Smalltalk language, which was developed at Xerox PARC, introduced the term Object-oriented programming to represent the pervasive use of objects and messages as the basis for computation. Smalltalk creators were influenced by the ideas introduced in Simula 67, but Smalltalk was designed to be a fully dynamic system in which objects could be created, modified, and 'consumed' "on the fly" rather than having a system based on static objects. It also introduced the notion of 'inheritance.' (Thus, Smalltalk was clearly a major move beyond the analog programming models, which made no use of "instances of classes," or even or Simula, which made no use of the "inheritance property.")

The ideas in Simula 67 were also used in many other languages, from derivatives of Lisp to Pascal.

Object-oriented programming developed as the dominant programming methodology during the mid-1980s, largely due to the influence of C++, an extension of the C programming language. Its dominance was further cemented by the rising popularity of Graphical user interfaces, for which object-oriented programming is allegedly well-suited. An example of a closely related dynamic GUI library and OOP language can be found in the Cocoa frameworks on Mac OS X, written in Objective C, an object-oriented, dynamic messaging extension to C based on Smalltalk. OOP toolkits also enhanced the popularity of "event-driven programming" (although this concept is not limited to OOP).

At ETH Zürich, Niklaus Wirth and his colleagues had also been investigating such topics as data abstraction and modular programming. Modula-2 included both, and their succeeding design, Oberon included a distinctive approach to object orientation, classes, and such. The approach is unlike Smalltalk, and very unlike C++.

Object-oriented features have been added to many existing languages during that time, including Ada, BASIC, Lisp, Fortran, Pascal, and others. Adding these features to languages that were not initially designed for them often led to problems with compatibility and maintainability of code. "Pure" object-oriented languages, on the other hand, lacked features that many programmers had come to depend upon. To bridge this gap, many attempts have been made to create new languages based on object-oriented methods but allowing some procedural features in "safe" ways. Bertrand Meyer's Eiffel was an early and moderately successful language with those goals.

In the past decade Java has emerged in wide use partially because of its similarity to C and to C++, but perhaps more importantly because of its implementation using a virtual machine that is intended to run code unchanged on many different platforms. This last feature has made it very attractive to larger development shops with heterogeneous environments. Microsoft's .NET initiative has a similar objective and includes/supports several new languages, or variants of older ones.

More recently, a number of languages have emerged that are primarily object-oriented yet compatible with procedural methodology, such as Python and Ruby. Besides Java, probably the most commercially important recent object-oriented languages are Visual Basic .NET and C# designed for Microsoft's .NET platform.

Just as procedural programming led to refinements of techniques such as structured programming, modern object-oriented software design methods include refinements such as the use of design patterns, design by contract, and modeling languages (such as UML).

Learning

There have been many disputes as to the best language to begin with when first learning object oriented programming. There are two different approaches most take when beginning object oriented programming. The first is the idea that it is best to start with a simpler language such as Java, where the learner's focus on object-orientation is not distracted by complex language semantics. Still, others reason that it is best to start off with a more complicated language such as C++, which more accurately supports all the structures and capabilities as prescribed by the Unified Modeling Language (UML). Note that there is no known language which inherently supports the full set of capabilities prescribed by UML.

Further reading


See also

External links

Criticism

Personal tools