Object-oriented programming (OOP) is a computer programming paradigm that emphasizes the following aspects:
- 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 it's manipulating, i.e. the ability to focus on the essential. 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:
- Encapsulation - Also called information hiding: Ensures that objects cannot change the internal state of other objects in unexpected ways; only the object's own internal methods are allowed to access its state. Each type of object exposes an interface to other objects that specifies how other objects may interact with it. This prevents users from breaking the invariants of the program.
- Polymorphism - References to and collections of objects may refer to objects of different types, and invoking an operation on a reference will produce behavior depending on the actual type of the referent.
- 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. This is typically done by grouping objects into classes, and defining classes as extensions of existing classes, thus and grouping classes into trees or lattices reflecting behavioral commonality.
It is in fact a set of ideas which mostly existed before. 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.
The exact definitions of these have some variation depending on point of view.
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.
Basics of object-oriented programming
The fundamental principle of object-oriented programming is that a computer program is composed of a collection of individual units, or objects which can function like sub-programs. To make the overall computation happen, each object is capable of receiving messages, processing data, and sending messages to other objects. In short, the objects can interact through their own functions (or methods) and their own data.
In this way, messages can be handled, as appropriate, by one chunk of code or by many in a seamless way. It is claimed that this gives more flexibility over simple step-by-step programming, called imperative programming or structured programming in the field of computer science.
Proponents of OOP also claim that OOP is more intuitive and that it is easier to learn, for those new to computer programming, than previous approaches. In OOP, objects are simple, self contained and easily identifiable. This modularity allows the program parts to correspond to real aspects of the problem and thereby to model the real world. Object-oriented programming often begins 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. This allows one to learn how to program in object-oriented languages.
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 scheme s.
Despite this, there is general recognition that the OOP approach is often simpler to develop and to maintain, lending itself to more intuitive analysis, coding, and understanding of complex situations and procedures.
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.
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.
The definitions of OOP are disputed. In the most general sense, object-oriented programming refers to the practice of viewing software primarily in terms of the "things" (objects) it manipulates, rather than the actions it performs. Other paradigms such as functional and procedural programming focus primarily on the actions, with the objects being secondary considerations; in OOP, the situation is reversed.
One distinguishing feature of OOP is the handling of subtypes of data types. Objects' data is generally required to satisfy programmer-defined constraints (e.g. class invariants). 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 most popular and developed model of OOP is a class-based model, as opposed to an object-based model. In this model, objects are entities that combine state (i.e., data), behavior (i.e., procedures, or methods) and identity (unique existence among all other objects). The structure and behavior of an object are defined by a class, which is a definition, or blueprint, of all objects of a specific type. An object must be explicitly created based on a class and an object thus created is considered to be an instance of that class. An object is similar to a structure, with the addition of method pointers, member access control, and an implicit data member which locates instances of the class (i.e. actual objects of that class) in the class hierarchy (essential for runtime inheritance features).
One object's data and/or functionality may be based on those of other objects, from which the former object is said to inherit. This allows commonalities among different kinds of objects to be expressed once and reused multiple times. Inheritance is also commonly held to include subtyping, whereby one type of object is defined to be a more specialised version of another type (see Liskov substitution principle), though non-subtyping inheritance is also possible. Inheritance is typically expressed by describing classes of objects arranged in an inheritance hierarchy reflecting common behavior.
Critique of class-based models
Class-based languages, or, to be more precise, typed languages, where subclassing is the only way of subtyping, have been criticized for mixing up implementations and interfaces—the essential principle in object-oriented programming. It says one might create a bag class that stores a collection of objects, then extends it to make a new class called a set class where the duplication of objects is eliminated. Now, a function that takes a bag class may expect that adding two objects increases the size of a bag by two, yet if one passes an object of a set class, then adding two objects may or may not increase the size of a bag by two. The problem arises precisely because subclassing implies subtyping even in the instances where the principle of subtyping, known as the Liskov Substitution Principle, does not hold.
Also, another common example is that a person object created from a child class cannot become an object of parent class because a child class and a parent class inherit a person class but class-based languages mostly do not allow to change the kind of class of the object at runtime.
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. SELF, a programming language developed by Sun Microsystems is an instance of a language that uses prototyping for behavior sharing rather than classification. Prothon is similar to self, except for having a python-like syntax. NewtonScript, Act1 , IoLanguage 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 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 is not OOP.
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.
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 1960's. It's 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".
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
- records and record extension s
- F-bounded polymorphism
Scripting and OOP
The concept of objects and instances in computing had its first major breakthrough with Sketchpad made by Ivan Sutherland in 1963. However this was an application and not a programming paradigm. The object-oriented programming paradigm first took root in 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.) They were later refined in Smalltalk, which was developed in Simula 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, 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).
Main article: Object-oriented programming language
Category:Object-oriented programming languages provides an exhaustive list.
- Grady Booch: Object-Oriented Analysis and Design with Applications, Addison-Wesley, ISBN 0805353402
- 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 Jacobsen : 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
- Programming paradigms
- Software componentry
- Interface description language
- Object-oriented programming language
- Distributed programming
- glossary of object-oriented programming
- How to program in object-oriented languages
- Design pattern (computer science)