Object-oriented programming
|
In computer science, object-oriented programming, OOP for short, is a computer programming paradigm that emphasizes the following concepts:
- Objects - Packaging data and functionality together into units within a running computer program; objects are the basis of modularity and structure in an object-oriented computer program.
- Abstraction - The ability for a program to ignore some aspects of the information that it is manipulating, i.e. the ability to focus on the essential.
- Encapsulation - Ensures that users of an object cannot change the internal state of the object in unexpected ways; only the object's own internal methods are allowed to access its state. Each object exposes an interface that specifies how other objects may interact with it. Other objects will not know and thus rely upon this object's internal representation.
- Polymorphism via message sending. Instead of subroutine calls, object-oriented languages can make message sends; the specific method which responds to a message send depends on what specific object the message is sent to. This gains polymorphism, because a single variable in the program text can hold different kinds of objects as the program runs, and thus the same program text can invoke different methods at different times in the same execution. To contrast, functional languages gain polymorphism through the use of first-class functions.
- Inheritance- Organizes and facilitates polymorphism and encapsulation by permitting objects to be defined and created that are specialized types of already-existing objects - these can share (and extend) their behavior without having to reimplement that behavior. (Object-based languages do not always have inheritance.)
Contents [hide] |
Overview
The idea behind object-oriented programming is that a computer program is composed of a collection of individual units, or objects, as opposed to a traditional view in which a program is little more than a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects. In this way, messages can be handled, as appropriate, by one chunk of code or by many in a seamless way. Thus, it is claimed that this gives more flexibility, easing making changes to the program.
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.
In OOP, objects are self contained and easily identifiable. This modularity allows the program parts to correspond to aspects of the problem. Object-oriented programming can begin from a written statement of the problem situation. Then by a process of inserting objects or variables for nouns, methods for verbs and attributes for adjectives, a good start is made on a framework for a program that models, and deals with, that situation.
Aspects
Abstraction: Each object in the system serves as a model of an abstract "actor" that can perform work, report on and change its state, and "communicate" with other objects in the system, without revealing how these features are implemented. Processes, functions or methods may also be so abstracted, and when they are, a variety of techniques are required to extend an abstraction:
Polymorphism: A Greek prefix poly means multi or many and a suffix -morphism a specified form. In OOP, polymorphism denotes a situation in which an object can be seen in more than one form. This form may mean a datatype in a typed language, but may mean something else in an untyped language. For example, if a bird receives the message "move fast", it will flap its wings and fly. If a lion receives the same message, it will move its legs and run. Both answer the same request, but in ways appropriate to each creature.
OOP as a new paradigm, point of view or marketing term
OOP has been 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 is always subject-oriented and since the requirements for the subject is 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 view 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 a benefit often attributed to OOP.
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 by Hewitt, in which as the overall computation happens, each actor is capable of receiving messages, processing data, and sending messages to other actors.
In fact, almost all OOP languages and systems, including all the major ones such as SIMULA, Smalltalk-80, C++, Java, and Python are based on the imperative model of computation, a few on the functional and logical models, and extremely few on 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 decompositing 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.
Disadvantages
However, it is recognized that OOP does not necessarily mean lack of complexity. Meta class programming (see Meta class) 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.
Objective verification of such benefits beyond narrow niches is elusive. It is often heavily debated whether the benefits of OOP are based on universal truths or based on a fit to human psychology. "Would aliens prefer OOP?" is sometimes used as a thought-provoking question. Consider that the difficulties in managing inter-object responsibilities and cooperation are strikingly similar to those in how we struggle with inter-personal communication. It is quite reasonable to assume that the model may be very appropriate for humans at our current state of social development (all else we leave to discussions on social archetypes). However, it must be noted that thinking patterns can vary widely per individual.
Implementation techniques
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.
Class-based models
See Class-based OOP.
Prototype-based models
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.
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. Some people regard OBP as not 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.
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, instead using OO techniques such as "composition". A simple way of avoiding over-specification of hierarchies when modelling the real world is to consider the most specific types of objects and model relationships between those.
While OOP is popular for managing access to lower-level or external services, it has proven more difficult and inconsistent for "business modeling". 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 modelling is based fundamentally on different premises than object-oriented modelling. 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 modelling, 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".
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:
- coalgebraic datatypes
- existential quantification and modules
- recursion
- records and record extensions
- F-bounded polymorphism
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").
Scripting and OOP
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 scripting language has been slowly adding new object oriented features since version 5. 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 indication was Sketchpad made by Ivan Sutherland in 1963, however this was an application and not a programming paradigm.
The first object-oriented programming language was Simula 67, a 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.)
The ideas in that language were then used in many other languages, from derivatives of Lisp and Pascal to the Smalltalk family of languages, which was developed at Xerox PARC, but was designed to be a fully dynamic system in which objects could be created and modified "on the fly" rather than having a system based on static programs.
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 Zurich, 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 modelling languages (such as UML).
Example languages
Main article: Object-oriented programming language
- Simula
- Smalltalk
- Objective-C
- C #
- C++
- Object Pascal
- Common Lisp Object System
- Eiffel
- Fortran 2003
- Java
- Ruby
- Python
- Perl - later versions
- PHP - later versions
- Ocaml
Category:Object-oriented programming languages provides an extensive list.
Further reading
- Grady Booch: Object-Oriented Analysis and Design with Applications, Addison-Wesley, ISBN 0805353402
- Alan Kay: The Early History of Smalltalk (http://gagne.homedns.org/%7etgagne/contrib/EarlyHistoryST.html)
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns: Elements of Reusable Object Oriented Software, Addison-Wesley, ISBN 0201633612
- Bertrand Meyer: Object-Oriented Software Construction, Prentice Hall, ISBN 0136291554
- James Rumbaugh, Michael Blaha, William Premerlani, Frederick Eddy, William Lorensen: Object-Oriented Modeling and Design, Prentice Hall, ISBN 0136298419
- Ivar Jacobson: Object-Oriented Software Engineering: A Use Case-Driven Approach, Addison-Wesley, ISBN 0201544350
- Harold Abelson, Gerald Jay Sussman, Julie Sussman: Structure and Interpretation of Computer Programs, The MIT Press, ISBN 0262011530
- Martin Abadi, Luca Cardelli: A Theory of Objects, Springer-Verlag, ISBN 0-387-94775-2
- Paul Harmon, William Morrissey: The Object Technology Casebook - Lessons from Award-Winning Business Applications, John Wiley & Sons, ISBN 0-471-14717-6
- David A. Taylor: Object-Oriented Information Systems - Planning and Implementation, John Wiley & Sons, ISBN 0-471-54364-0
- Peter Eeles, Oliver Sims: Building Business Objects, John Wiley & Sons, ISBN 0-471-19176-0
See also
- Programming paradigms
- Software componentry
- Interface description language
- Object-oriented programming language
- Distributed programming
- List of object-oriented programming terms
- Design pattern (computer science)
- Refactoring
- CORBA
- Globus
- DCOM
External links
- Object-oriented programming FAQ (http://www.objectfaq.com/oofaq2/)
- Example of the subtyping problem (http://okmij.org/ftp/Computation/Subtyping/#Problem)
- What are OOP's Jargons and Complexities (http://xahlee.org/Periodic_dosage_dir/t2/oop.html)
- Object Oriented Design Principles (http://www.eventhelix.com/RealtimeMantra/Object_Oriented/)
- The report "Object Orientation Redefined" (http://www.interaction-design.org/mads/articles/object_orientation_redefined.html) outlines how OO has been considered a thinking tool, i.e. "we view the world in objects", and how such an approach may further our understanding of object-oriented formalisation.
Criticism
- Criticism of OOP (http://www.geocities.com/tablizer/oopbad.htm)fr:Programmation orientée objet
he:�аммирование fi:Olio-ohjelmointi sv:Objektorienterad programmering th:การเขียนโปรแกรมเชิงวัตถุ zh:面向对象的程序设计