Object-oriented programming can trace its roots to the 1960s. As hardware and software became increasingly complex, quality was often compromised. Researchers studied ways in which software quality could be maintained. Object-oriented programming was deployed in part as an attempt to address this problem by strongly emphasizing discrete units of programming logic and re-usability in software. Computer programming methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) containing all the information needed within its own data structure for manipulation.
The Simula programming language was the first to introduce the concepts underlying object-oriented programming (objects, classes, subclasses, virtual methods, coroutines, garbage collection, and discrete event simulation) as a superset of Algol. Simula was used for physical modeling, such as models to study and improve the movement of ships and their content through cargo ports. Smalltalk was the first programming language to be called "object-oriented".
Object-oriented programming may be seen as a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a group of tasks to compute ("subroutines"). In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects.
Each object can be viewed as an independent little machine with a distinct role or responsibility. The actions or "operators" on the objects are closely associated with the object. For example, in object oriented programming, the data structures tend to carry their own operators around with them (or at least "inherit" them from a similar object or "class"). The traditional approach tends to view and consider data and behavior separately.
Dogwould consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of the class should make sense in context. Also, the code for a class should be relatively self-contained (generally using encapsulation). Collectively, the properties and methods defined by a class are called members. Object : A pattern (exemplar) of a class. The class of
Dogdefines all possible dogs by listing the characteristics and behaviors they can have; the object
Lassieis one particular dog, with particular versions of the characteristics. A
Lassiehas brown-and-white fur. Instance : One can have an instance of a class or a particular object. The instance is the actual object created at runtime. In programmer jargon, the
Lassieobject is an instance of the
Dogclass. The set of values of the attributes of a particular object is called its state. The object consists of state and the behaviour that's defined in the object's class. Method : An object's abilities. In language, methods are verbs.
Lassie, being a
Dog, has the ability to bark. So
bark()is one of
Lassie's methods. She may have other methods as well, for example
save_timmy(). Within the program, using a method usually affects only one particular object; all
Dogs can bark, but you neeit'' attributes and behaviors from their parent classes, and can introduce their own.
Dogmight have sub-classes called
GoldenRetriever. In this case,
Lassiewould be an instance of the
Colliesubclass. Suppose the
Dogclass defines a method called
bark()and a property called
furColor. Each of its sub-classes (
GoldenRetriever) will inherit these members, meaning that the programmer only needs to write the code for them once.
Collieclass might specify that the default
furColorfor a collie is brown-and-white. The
Chihuahuasubclass might specify that the
bark()method produces a high pitch by default. Subclasses can also add new members. The
Chihuahuasubclass could add a method called
tremble(). So an individual chihuahua instance would use a high-pitched
Chihuahuasubclass, which in turn inherited the usual
Dog. The chihuahua object would also have the
Lassiewould not, because she is a
Collie, not a
Chihuahua. In fact, inheritance is an ‘is-a’ relationship:
Lassieinherits the methods of both
Cats, and a
Chimeraobject could be created from these two which inherits all the (multiple) behavior of cats and dogs. This is not always supported, as it can be hard both to implement and to use well. Abstraction : Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
Dogmay be treated as a
Dogmuch of the time, a
Colliewhen necessary to access
Collie-specific attributes or behaviors, and as an
Animal(perhaps the parent class of
Dog) when counting Timmy's pets.
Carwould be made up of an Engine, Gearbox, Steering objects, and many more components. To build the
Carclass, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other. Encapsulation : Encapsulation conceals the functional details of a class from objects that send messages to it.
Dogclass has a
bark()method. The code for the
bark()method defines exactly how a bark happens (e.g., by
exhale(), at a particular pitch and volume). Timmy,
Lassie's friend, however, does not need to know exactly how she barks. Encapsulation is achieved by specifying which classes may use the members of an object. The result is that each object exposes to any class a certain interface — those members accessible to that class. The reason for encapsulation is to prevent clients of an interface from depending on those parts of the implementation that are likely to change in future, thereby allowing those changes to be made more easily, that is, without changes to clients. For example, an interface can ensure that puppies can only be added to an object of the class
Dogby code in that class. Members are often specified as public, protected or private, determining whether they are available to all classes, sub-classes or only the defining class. Some languages go further: Java uses the default access modifier to restrict access also to classes in the same package, C# and VB.NET reserve some members to classes in the same assembly using keywords internal (C#) or Friend (VB.NET), and Eiffel and C++ allow one to specify which classes may access any member. Polymorphism : Polymorphism allows the programmer to treat derived class members just like their parent class' members. More precisely, Polymorphism in object-oriented programming is the ability of objects belonging to different data types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behavior. One method, or an operator such as +, -, or *, can be abstractly applied in many different situations. If a
Dogis commanded to
speak(), this may elicit a
bark(). However, if a
is commanded to
speak(), this may elicit an
oink(). They both inherit
Animal, but their derived class methods override the methods of the parent class; this is Overriding Polymorphism. Overloading Polymorphism is the use of one method signature, or one operator such as ‘+’, to perform several different functions depending on the implementation. The ‘+’ operator, for example, may be used to perform integer addition, float addition, list concatenation, or string concatenation. Any two subclasses of
Number, such as
Double, are expected to add together properly in an OOP language. The language must therefore overload the concatenation operator, ‘+’, to work this way. This helps improve code readability. How this is implemented varies from language to language, but most OOP languages support at least some level of overloading polymorphism. Many OOP languages also support Parametric Polymorphism, where code is written without mention of any specific type and thus can be used transparently with any number of new types. Pointers are an example of a simple polymorphic routine that can be used with many different types of objects. Decoupling : Decoupling allows for the separation of object interactions from classes and inheritance into distinct layers of abstraction. A common use of decoupling is to polymorphically decouple the encapsulation, which is the practice of using reusable code to prevent discrete code modules from interacting with each other.
Not all of the above concepts are to be found in all object-oriented programming languages, and so object-oriented programming that uses classes is called sometimes class-based programming. In particular, prototype-based programming does not typically use classes. As a result, a significantly different yet analogous terminology is used to define the concepts of object and instance, although there are no objects in these languages.
The Smalltalk language, which was developed at Xerox PARC in the 1970s, 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 classes could be created and modified dynamically rather than statically as in Simula 67. Smalltalk and with it OOP were introduced to a wider audience by the August 1981 issue of Byte magazine.
In the 1970s, Kay's Smalltalk work had influenced the Lisp community to incorporate object-based techniques which were introduced to developers via the Lisp machine. In the 1980s, there were a few attempts to design processor architectures which included hardware support for objects in memory but these were not successful. Examples include the Intel iAPX 432 and the Linn Smart Rekursiv.
Object-oriented programming developed as the dominant programming methodology during the mid-1990s, largely due to the influence of C++. Its dominance was further enhanced by the rising popularity of graphical user interfaces, for which object-oriented programming seems to be 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). Some feel that association with GUIs (real or perceived) was what propelled OOP into the programming mainstream.
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, 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.
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 with the important caveat that it is, of course, restricted to the Microsoft platform.
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 (VB.NET) and C#, both designed for Microsoft's .NET platform. VB.NET and C# both support cross-language inheritance, allowing classes defined in one language to subclass classes defined in the other language.
Recently many universities have begun to teach Object-oriented design in introductory computer science classes.
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).
There are also object databases which can be used to replace RDBMSs, but these have not been as commercially successful as RDBMSs.
However, Niklaus Wirth said of OOP in his paper "Good Ideas through the Looking Glass", "This paradigm closely reflects the structure of systems 'in the real world', and it is therefore well suited to model complex systems with complex behaviours."
Attempts to find a consensus definition or theory behind objects have not proven very successful (however, see "Abadi & Cardelli: A Theory of Objects" for formal definitions of many OOP concepts and constructs), 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").
... what society overwhelmingly asks for is snake oil. Of course, the snake oil has the most impressive names —otherwise you would be selling nothing— like "Structured Analysis and Design", "Software Engineering", "Maturity Models", "Management Information Systems", "Integrated Project Support Environments" "Object Orientation" and "Business Process Re-engineering" (the latter three being known as IPSE, OO and BPR, respectively)." — EWD 1175: The strengths of the academic enterprise