UML notation for a class. This Button class hasvariables for data, andfunctions. Through inheritance, a subclass can be created as a subset of the Button class. Objects are instances of a class.
Object-oriented programming (OOP) is aprogramming paradigm based onobjects[1] –software entities thatencapsulatedata andfunction(s). An OOPcomputer program consists of objects that interact with one another.[2][3] AnOOPlanguage is one that provides object-oriented programming features, but as the set of features that contribute to OOP is contested, classifying a language as OOP – and the degree to which it supports OOP – is debatable. As paradigms are not mutually exclusive, a language can bemulti-paradigm (i.e. categorized as more than only OOP).
The idea of "objects" in programming began with theartificial intelligence group atMassachusetts Institute of Technology (MIT) in the late 1950s and early 1960s. Here, "object" referred toLISP atoms with identified properties (attributes).[5][6]Another early example wasSketchpad created byIvan Sutherland at MIT in 1960–1961. In the glossary of his technical report, Sutherland defined terms like "object" and "instance" (with the class concept covered by "master" or "definition"), albeit specialized to graphical interaction.[7] Later, in 1968, AED-0, MIT's version of theALGOL programming language, connected data structures ("plexes") and procedures, prefiguring what were later termed "messages", "methods", and "member functions".[8][9]Topics such asdata abstraction andmodular programming were common points of discussion at this time.
Meanwhile, in Norway,Simula was developed during the years 1961–1967.[8] Simula introduced essential object-oriented ideas, such asclasses, inheritance, anddynamic binding.[10]Simula was used mainly by researchers involved withphysical modelling, like the movement of ships and their content through cargo ports.[10] Simula is generally accepted as being the first language with the primary features and framework of an object-oriented language.[11]
I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning – it took a while to see how to do messaging in a programming language efficiently enough to be useful).
Influenced by both MIT and Simula,Alan Kay began developing his own ideas in November 1966. He would go on to createSmalltalk, an influential OOP language. By 1967, Kay was already using the term "object-oriented programming" in conversation.[1] Although sometimes called the "father" of OOP,[12] Kay has said his ideas differ from how OOP is commonly understood, and has implied that the computer science establishment did not adopt his notion.[1]A 1976 MIT memo co-authored byBarbara Liskov listsSimula 67,CLU, andAlphard as object-oriented languages, but does not mention Smalltalk.[13]
In the 1970s, the first version of theSmalltalk programming language was developed atXerox PARC byAlan Kay,Dan Ingalls andAdele Goldberg. Smalltalk-72 was notable for use of objects at the language level and its graphical development environment.[14] Smalltalk was a fully dynamic system, allowing users to create and modify classes as they worked.[15] Much of the theory of OOP was developed in the context of Smalltalk, for example multiple inheritance.[16]
In the late 1970s and 1980s, OOP rose to prominence. TheFlavors object-oriented Lisp was developed starting 1979, introducingmultiple inheritance andmixins.[17] In August 1981,Byte Magazine highlighted Smalltalk and OOP, introducing these ideas to a wide audience.[18] LOOPS, the object system forInterlisp-D, was influenced by Smalltalk and Flavors, and a paper about it was published in 1982.[19] In 1986, the firstConference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA) was attended by 1,000 people. This conference marked the start of efforts to consolidate Lisp object systems, eventually resulting in theCommon Lisp Object System. In the 1980s, there were a few attempts to designprocessor architectures that includedhardware support for objects inmemory, but these were not successful. Examples include theIntel iAPX 432 and theLinn SmartRekursiv.
AtETH Zürich,Niklaus Wirth and his colleagues created new approaches to OOP.Modula-2 (1978) andOberon (1987), included a distinctive approach to object orientation, classes, and type checking across module boundaries. Inheritance is not obvious in Wirth's design since his nomenclature looks in the opposite direction: It is called type extension and the viewpoint is from the parent down to the inheritor.
Many programming languages that were initially developed before OOP was popular have been augmented with object-oriented features, includingAda,BASIC,Fortran,Pascal, andCOBOL.
The OOP features provided by languages varies. Below are some common features of OOP languages.[24][25][26][27] Comparing OOP with other styles, likerelational programming, is difficult because there isn't a clear, agreed-upon definition of OOP.[28]
Cohesion, keeping relatedfields andmethods together. A field (a.k.a. attribute or property) contains information (a.k.a. state) as avariable. A method (a.k.a.function or action) defines behavior via logic code.
Decoupling, organizing code so that only certain parts of the data are used by related functions. Decoupling makes it easier to change how an object works on the inside without affecting other parts of thecodebase, such as incode refactoring.[29] Objects act as a boundary between their internal workings and external, consuming code.
Data hiding, keeping the internal details of an object hidden from outside code. Consuming code can only interact with an object via its public members, due to the language providingaccess modifiers that control visibility.
Some programming languages, like Java, provide information hiding via visibility key words (private andpublic).[30] Some languages like Python don't provide a visibility feature, but developers might follow a convention such as starting a private member name with an underscore. Intermediate levels of access also exist, such as Java'sprotected keyword, (which allows access from the same class and its subclasses, but not objects of a different class), and theinternal keyword in C#, Swift, and Kotlin, which restricts access to files within the same module.[31]
Supporters of information hiding and data abstraction say it makes code easier to reuse and intuitively represents real-world situations.[32][33] However, others argue that OOP does not enhance readability or modularity.[34][35]Eric S. Raymond has written that OOP languages tend to encourage thickly layered programs that destroy transparency.[36] Raymond compares this unfavourably to the approach taken withUnix and theC language.[36]
SOLID includes theopen/closed principle, which says that classes and functions should be "open for extension, but closed for modification".Luca Cardelli has stated that OOP languages have "extremely poor modularity properties with respect to class extension and modification", and tend to be extremely complex.[34] The latter point is reiterated byJoe Armstrong, the principal inventor ofErlang, who is quoted as saying:[35]
The problem with object-oriented languages is they've got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.
Inclass-based programming, the most common type of OOP, an object is an instance of a class. The class defines the data (variables) and methods (logic). An object is created via theconstructor. Every instance of the class has the same set of variables and methods. Elements may include:
Class variable – belongs to the class itself; all objects of the class share one copy
Instance variable – belongs to an object; every object has its own version of these variables
Member variable – refers to both the class and instance variables of a class
Class method – can only use class variables
Instance method – belongs to an object; can use both instance and class variables
Classes may inherit from other classes, creating a hierarchy of classes: a case of a subclass inheriting from a super-class. For example, anEmployee class might inherit from aPerson class which endows the Employee object with the variables fromPerson. The subclass may add variables and methods that do not affect the super-class. Most languages also allow the subclass to override super-class methods. Some languages supportmultiple inheritance, where a class can inherit from more than one class, and other languages similarly supportmixins ortraits. For example, a mixin called UnicodeConversionMixin might add a method unicode_to_ascii() to both a FileReader and a WebPageScraper class.
Anabstract class cannot be directly instantiated as an object. It is only used as a super-class.
Other classes are utility classes which contain only class variables and methods and are not meant to be instantiated or subclassed.[39]
Instead of providing a class concept, inprototype-based programming, an object is linked to another object, called itsprototype orparent. In Self, an object may have multiple or no parents,[40] but in the most popular prototype-based language,JavaScript, an object has exactly one prototype link, up to the base object whose prototype is null.
A prototype acts as a model for new objects. For example, if you have an objectfruit, you can make two objectsapple andorange that share traits of thefruit prototype. Prototype-based languages also allow objects to have their own unique properties, so theapple object might have an attributesugar_content, while theorange orfruit objects do not.
In all OOP languages, viaobject composition, an object can contain other objects. For example, anEmployee object might contain anAddress object, along with other information likename andposition. Composition is a "has-a" relationships, like "an employee has an address". Some languages, likeGo, don't support inheritance.[41] Instead, they encourage "composition over inheritance", where objects are built using smaller parts instead of parent-child relationships. For example, instead of inheriting from class Person, the Employee class could simply contain a Person object. This lets the Employee class control how much of Person it exposes to other parts of the program.Delegation is another language feature that can be used as an alternative to inheritance.
Programmers have different opinions on inheritance. Bjarne Stroustrup, author of C++, has stated that it is possible to do OOP without inheritance.[42]Rob Pike has criticized inheritance for creating complex hierarchies instead of simpler solutions.[43]
People often think that if one class inherits from another, it means the subclass "is a" more specific version of the original class. This presumes theprogram semantics are that objects from the subclass can always replace objects from the original class without problems. This concept is known asbehavioral subtyping, more specifically theLiskov substitution principle.
However, this is often not true, especially in programming languages that allowmutable objects, objects that change after they are created. In fact,subtype polymorphism as enforced by thetype checker in OOP languages cannot guarantee behavioral subtyping in most if not all contexts. For example, thecircle-ellipse problem is notoriously difficult to handle using OOP's concept of inheritance. Behavioral subtyping is undecidable in general, so it cannot be easily implemented by a compiler. Because of this, programmers must carefully design class hierarchies to avoid mistakes that the programming language itself cannot catch.
A method may be invoked viadynamic dispatch such that the method is selected at runtime instead of compile time. If the method choice depends on more than one type of object (such as other objects passed as parameters), it's calledmultiple dispatch. In this context, a method call is also known asmessage passing, meaning the method name and its inputs are like a message sent to the object for it to act on.[44]
Dynamic dispatch works together with inheritance: if an object doesn't have the requested method, it looks up to its parent class (delegation), and continues up the chain to find a matching method.
Polymorphism in OOP refers tosubtyping or subtype polymorphism, where a function can work with a specificinterface and thus manipulate entities of different classes in a uniform manner.[45]
For example, imagine a program has two shapes: a circle and a square. Both come from a common class called "Shape." Each shape has its own way of drawing itself. With subtype polymorphism, the program doesn't need to know the type of each shape, and can simply call the "Draw" method for each shape. The programming language runtime will ensure the correct version of the "Draw" method runs for each shape. Because the details of each shape are handled inside their own classes, this makes the code simpler and more organized, enabling strongseparation of concerns.
An object's methods can access the object's data. Many programming languages use a special word, likethis orself, to refer to the current object. In languages that supportopen recursion, a method in an object can call other methods in the same object, including itself, using this special word. This allows a method in one class to call another method defined later in a subclass, a feature known aslate binding.
Design patterns are common solutions to problems in software design. Some design patterns are especially useful for OOP, and design patterns are typically introduced in an OOP context.
Sometimes, objects represent real-world things and processes in digital form.[46] For example, a graphics program may have objects such ascircle,square, andmenu. An online shopping system might have objects such asshopping cart,customer, andproduct.Niklaus Wirth said, "This paradigm [OOP] closely reflects the structure of systems in the real world and is therefore well suited to model complex systems with complex behavior".[47]
However, more often, objects represent abstract entities, like an open file or a unit converter. Not everyone agrees that OOP makes it easy to copy the real world exactly or that doing so is even necessary.Bob Martin suggests that because classes are software, their relationships don't match the real-world relationships they represent.[48]Bertrand Meyer argues that a program is not a model of the world but a model of some part of the world; "Reality is a cousin twice removed".[49]Steve Yegge noted thatnatural languages lack the OOP approach of naming a thing (object) before an action (method), as opposed tofunctional programming which does the reverse.[50] This can make an OOP solution more complex than one written viaprocedural programming.[51]
To solve this problem, developers use different methods, but none of them are perfect.[53] One of the most common solutions isobject-relational mapping (ORM), which helps connect object-oriented programs to relational databases. Examples of ORM tools includeVisual FoxPro,Java Data Objects, andRuby on Rails ActiveRecord.
Some databases, calledobject databases, are designed to work with OOP. However, they have not been as popular or successful as relational databases.
Date and Darwen have proposed a theoretical foundation that uses OOP as a kind of customizabletype system to support RDBMSs, but it forbids objects containing pointers to other objects.[54]
Inresponsibility-driven design, classes are built around what they need to do and the information they share, in the form of a contract. This is different fromdata-driven design, where classes are built based on the data they need to store. According to Wirfs-Brock and Wilkerson, the originators of responsibility-driven design, responsibility-driven design is the better approach.[55]
Open/closed principle: Software entities should be open for extension, but closed for modification.
Liskov substitution principle: Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
GRASP (General Responsibility Assignment Software Patterns) is another set of software design rules, created byCraig Larman, that helps developers assign responsibilities to different parts of a program:[56]
Creator Principle: allows classes create objects they closely use.
Information Expert Principle: assigns tasks to classes with the needed information.
Low Coupling Principle: reduces class dependencies to improve flexibility and maintainability.
High Cohesion Principle: designing classes with a single, focused responsibility.
Controller Principle: assigns system operations to separate classes that manage flow and interactions.
Polymorphism: allows different classes to be used through a common interface, promoting flexibility and reuse.
Pure Fabrication Principle: create helper classes to improve design, boost cohesion, and reduce coupling.
Researchers have tried to formally define thesemantics of OOP.Inheritance presents difficulties, particularly with the interactions between open recursion and encapsulated state. Researchers have usedrecursive types andco-algebraic data types to incorporate essential features of OOP.[57] Abadi and Cardelli defined several extensions ofSystem F<: that deal with mutable objects, allowing bothsubtype polymorphism andparametric polymorphism (generics), and were able to formally model many OOP concepts and constructs.[58] Although far from trivial, static analysis of object-oriented programming languages such as Java is a mature field,[59] with several commercial tools.[60]
TheTIOBE programming language popularity index graph from 2002 to 2023. In the 2000s the object-orientedJava (orange) and theproceduralC (dark blue) competed for the top position.
Many popular programming languages, like C++, Java, and Python, use OOP. In the past, OOP was widely accepted,[61] but recently, some programmers have criticized it and prefer functional programming instead.[62] A study by Potok et al. found no major difference in productivity between OOP and procedural programming.[63]
Some believe that OOP places too much focus on using objects rather than onalgorithms anddata structures.[64][65] For example, programmerRob Pike pointed out that OOP can make programmers think more about type hierarchy than composition.[66] He has called OOP "theRoman numerals of computing".[67]Rich Hickey, creator ofClojure, described OOP as overly simplistic, especially when it comes to representing real-world things that change over time.[65]Alexander Stepanov said that OOP tries to fit everything into a single type, which can be limiting. He argued that sometimes we need multisorted algebras: families of interfaces that span multiple types, such as ingeneric programming. Stepanov also said that calling everything an "object" doesn't add much understanding.[64]
OOP was created to make code easier toreuse andmaintain.[68] However, it was not designed to clearly show the flow of a program's instructions. That was left to the compiler. As computers began using more parallel processing and multiplethreads, it became more important to understand and control how instructions flow. This is difficult to do with OOP.[69][70][71][72]
Paul Graham, a well-known computer scientist, believes big companies like OOP because it helps manage large teams of average programmers. He argues that OOP adds structure, making it harder for one person to make serious mistakes, but at the same time restrains smart programmers.[73]Eric S. Raymond, aUnix programmer andopen-source software advocate, argues that OOP is not the best way to write programs.[36]
Richard Feldman says that, while OOP features helped some languages stay organized, their popularity comes from other reasons.[74] Lawrence Krubner argues that OOP doesn't offer special advantages compared to other styles, like functional programming, and can complicate coding.[75]Luca Cardelli says that OOP is slower and takes longer to compile than procedural programming.[34]
^Kindler, E.; Krivy, I. (2011). "Object-Oriented Simulation of systems with sophisticated control".International Journal of General Systems.40 (3):313–343.doi:10.1080/03081079.2010.539975.
^Lewis, John; Loftus, William (2008). "1.6: Object-Oriented Programming".Java Software Solutions. Foundations of Programming Design (6th ed.). Pearson Education Inc.ISBN978-0-321-53205-3.
^Ivan E. Sutherland (May 1963).Sketchpad: a man-machine graphical communication system. AFIPS '63 (Spring): Proceedings of the May 21–23, 1963 Spring Joint Computer Conference. AFIPS Press. pp. 329–346.doi:10.1145/1461551.1461591.
^1995 (June)Visual FoxPro 3.0, FoxPro evolves from a procedural language to an object-oriented language. Visual FoxPro 3.0 introduces a database container, seamless client/server capabilities, support for ActiveX technologies, and OLE Automation and null support.Summary of Fox releases
^1995 Reviewers Guide to Visual FoxPro 3.0:DFpug.de
^Deborah J. Armstrong.The Quarks of Object-Oriented Development. A survey of nearly 40 years of computing literature identified several fundamental concepts found in the large majority of definitions of OOP, in descending order of popularity: Inheritance, Object, Class, Encapsulation, Method, Message Passing, Polymorphism, and Abstraction.
^John C. Mitchell,Concepts in programming languages, Cambridge University Press, 2003,ISBN0-521-78098-5, p.278. Lists: Dynamic dispatch, abstraction, subtype polymorphism, and inheritance.
^Michael Lee Scott,Programming language pragmatics, Edition 2, Morgan Kaufmann, 2006,ISBN0-12-633951-1, p. 470. Lists encapsulation, inheritance, and dynamic dispatch.
^Pierce, Benjamin (2002).Types and Programming Languages. MIT Press.ISBN978-0-262-16209-8., section 18.1 "What is Object-Oriented Programming?" Lists: Dynamic dispatch, encapsulation or multi-methods (multiple dispatch), subtype polymorphism, inheritance or delegation, open recursion ("this"/"self")
^C. J. Date, Introduction to Database Systems, 6th-ed., Page 650
^"Is Go an object-oriented language?". Retrieved13 April 2019.Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy.
^Tan, Tian; Li, Yue (12 July 2023).Tai-e: A Developer-Friendly Static Analysis Framework for Java by Harnessing the Good Designs of Classics. ISSTA 2023. pp. 1093–1105.doi:10.1145/3597926.3598120.
^Bhutani, Vikram; Toosi, Farshad Ghassemi; Buckley, Jim (1 June 2024). "Analysing the Analysers: An Investigation of Source Code Analysis Tools".Applied Computer Systems.29 (1):98–111.doi:10.2478/acss-2024-0013.
^Brucker, Achim D.; Wolff, Burkhart (2008). "Extensible Universes for Object-Oriented Data Models".ECOOP 2008 – Object-Oriented Programming. Lecture Notes in Computer Science. Vol. 5142. pp. 438–462.doi:10.1007/978-3-540-70592-5_19.ISBN978-3-540-70591-8.object-oriented programming is a widely accepted programming paradigm