==Features==
Object-oriented programming uses objects, but not all of the associated techniques and structures are supported directly in languages that claim to support OOP. It performs operations on operands. The features listed below are common among languages considered to be strongly class- and object-oriented (or [[multi-paradigm]] with OOP support), with notable exceptions mentioned.<ref name="ArmstrongQuarks">Deborah J. Armstrong. ''The Quarks of Object-Oriented Development''. A survey of nearly 40 years of computing literature which identified a number of 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.</ref><ref>[[John C. Mitchell]], ''Concepts in programming languages'', Cambridge University Press, 2003, {{ISBN|0-521-78098-5}}, p.278. Lists: Dynamic dispatch, abstraction, subtype polymorphism, and inheritance.</ref><ref>Michael Lee Scott, ''Programming language pragmatics'', Edition 2, Morgan Kaufmann, 2006, {{ISBN|0-12-633951-1}}, p. 470. Lists encapsulation, inheritance, and dynamic dispatch.</ref><ref name="pierce">{{Cite book|last=Pierce|first=Benjamin|title=Types and Programming Languages|publisher=MIT Press|year=2002|isbn=978-0-262-16209-8|title-link=Types and Programming Languages}}, 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")</ref>
{{See also|Comparison of programming languages (object-oriented programming)|List of object-oriented programming terms}}
===Shared with non-OOP languages===
* [[Variable (computer science)|Variables]] that can store information formatted in a small number of built-in [[data type]]s like [[Integer (computer science)|integers]] and alphanumeric [[Character (computing)|characters]]. This may include [[data structures]] like [[String (computer science)|strings]], [[List (abstract data type)|lists]], and [[hash table]]s that are either built-in or result from combining variables using [[Pointer (computer programming)|memory pointers]].
* Procedures – also known as functions, methods, routines, or [[subroutine]]s – that take input, generate output, and manipulate data. Modern languages include [[structured programming]] constructs like [[Loop (computing)|loops]] and [[Conditional (computer programming)|conditionals]].
[[Modular programming]] support provides the ability to group procedures into files and modules for organizational purposes. Modules are [[namespace]]d so identifiers in one module will not conflict with a procedure or variable sharing the same name in another file or module.
===Objects and classes===
Objects sometimes correspond to things found in the real world. For example, a graphics program may have objects such as "circle", "square", "menu". An online shopping system might have objects such as "shopping cart", "customer", and "product".<ref>{{cite book|last=Booch|first=Grady|title=Software Engineering with Ada|year=1986|publisher=Addison Wesley|isbn=978-0-8053-0608-8|page=220|url=https://en.wikiquote.org/wiki/Grady_Booch|quote=Perhaps the greatest strength of an object-oriented approach to development is that it offers a mechanism that captures a model of the real world.}}</ref> Sometimes objects represent more abstract entities, like an object that represents an open file, or an object that provides the service of translating measurements from U.S. customary to metric.
Each object is said to be an [[instance (computer science)|instance]] of a particular class (for example, an object with its name field set to "Mary" might be an instance of class Employee). Procedures in object-oriented programming are known as [[Method (computer science)|methods]]; variables are also known as [[Field (computer science)|fields]], members, attributes, or properties. This leads to the following terms:
* [[Class variable]]s – belong to the ''class as a whole''; there is only one copy of each one
* [[Instance variable]]s or attributes – data that belongs to individual ''objects''; every object has its own copy of each one
* Instance methods – belong to ''individual objects'', and have access to instance variables for the specific object they are called on, inputs, and class variables
Objects are accessed somewhat like variables with complex internal structure, and in many languages are effectively [[Pointer (computer programming)|pointers]], serving as actual references to a single instance of said object in memory within a heap or stack. They provide a layer of [[Abstraction (computer science)|abstraction]] which can be used to separate internal from external code. External code can use an object by calling a specific instance method with a certain set of input parameters, read an instance variable, or write to an instance variable. Objects are created by calling a special type of method in the class known as a [[Constructor (object-oriented programming)|constructor]]. A program may create many instances of the same class as it runs, which operate independently. This is an easy way for the same procedures to be used on different sets of data.
Object-oriented programming that uses classes is sometimes called [[class-based programming]], while [[prototype-based programming]] does not typically use classes. As a result, significantly different yet analogous terminology is used to define the concepts of ''object'' and ''instance''.
In [[Class-based programming|class-based languages]] the ''classes'' are defined beforehand and the ''objects'' are instantiated based on the classes. If two objects ''apple'' and ''orange'' are instantiated from the class ''Fruit'', they are inherently fruits and it is guaranteed that you may handle them in the same way; e.g. a programmer can expect the existence of the same attributes such as ''color'' or ''sugar_content'' or ''is_ripe''.
In [[Prototype-based programming|prototype-based languages]] the ''objects'' are the primary entities. No ''classes'' even exist. The ''prototype'' of an object is just another object to which the object is linked. Every object has one ''prototype'' link (and only one). New objects can be created based on already existing objects chosen as their prototype. You may call two different objects ''apple'' and ''orange'' a fruit, if the object ''fruit'' exists, and both ''apple'' and ''orange'' have ''fruit'' as their prototype. The idea of the ''fruit'' class doesn't exist explicitly, but as the [[equivalence class]] of the objects sharing the same prototype. The attributes and methods of the ''prototype'' are [[Delegation (object-oriented programming)|delegated]] to all the objects of the equivalence class defined by this prototype. The attributes and methods ''owned'' individually by the object may not be shared by other objects of the same equivalence class; e.g. the attribute ''sugar_content'' may be unexpectedly not present in ''apple''. Only [[single inheritance]] can be implemented through the prototype.
===Dynamic dispatch/message passing===
It is the responsibility of the object, not any external code, to select the procedural code to execute in response to a method call, typically by looking up the method at run time in a table associated with the object. This feature is known as [[dynamic dispatch]]. If the call variability relies on more than the single type of the object on which it is called (i.e. at least one other parameter object is involved in the method choice), one speaks of [[multiple dispatch]].
A method call is also known as ''[[message passing]]''. It is conceptualized as a message (the name of the method and its input parameters) being passed to the object for dispatch.
===Data abstraction===
Data abstraction is a design pattern in which data are visible only to semantically related functions, so as to prevent misuse. The success of data abstraction leads to frequent incorporation of [[Information hiding|data hiding]] as a design principle in object oriented and pure functional programming.
If a class does not allow calling code to access internal object data and permits access through methods only, this is a form of information hiding known as [[Abstraction (computer science)|abstraction]]. Some languages (Java, for example) let classes enforce access restrictions explicitly, for example denoting internal data with the <code>private</code> keyword and designating methods intended for use by code outside the class with the <code>public</code> keyword. Methods may also be designed public, private, or intermediate levels such as <code>protected</code> (which allows access from the same class and its subclasses, but not objects of a different class). In other languages (like Python) this is enforced only by convention (for example, <code>private</code> methods may have names that start with an [[underscore]]). In C#, Swift & Kotlin languages, <code>internal</code> keyword permits access only to files present in same assembly, package or module as that of the class.<ref>{{Cite web |date=2023-01-05 |title=What is Object Oriented Programming (OOP) In Simple Words? – Software Geek Bytes |url=https://softwaregeekbytes.com/object-oriented-programming-simple-words/ |access-date=2023-01-17 |language=en-US}}</ref>
===Encapsulation===
[[Encapsulation (computer programming)|Encapsulation]] prevents external code from being concerned with the internal workings of an object. This facilitates [[code refactoring]], for example allowing the author of the class to change how objects of that class represent their data internally without changing any external code (as long as "public" method calls work the same way). It also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages [[Coupling (computer programming)|decoupling]].
===Composition, inheritance, and delegation===
Objects can contain other objects in their instance variables; this is known as [[object composition]]. For example, an object in the Employee class might contain (either directly or through a pointer) an object in the Address class, in addition to its own instance variables like "first_name" and "position". Object composition is used to represent "has-a" relationships: every employee has an address, so every Employee object has access to a place to store an Address object (either directly embedded within itself, or at a separate ___location addressed via a pointer).
Languages that support classes almost always support [[inheritance (object-oriented programming)|inheritance]]. This allows classes to be arranged in a hierarchy that represents "is-a-type-of" relationships. For example, class Employee might inherit from class Person. All the data and methods available to the parent class also appear in the child class with the same names. For example, class Person might define variables "first_name" and "last_name" with method "make_full_name()". These will also be available in class Employee, which might add the variables "position" and "salary". This technique allows easy re-use of the same procedures and data definitions, in addition to potentially mirroring real-world relationships in an intuitive way. Rather than utilizing database tables and programming subroutines, the developer utilizes objects the user may be more familiar with: objects from their application ___domain.<ref>{{cite book|last=Jacobsen|first=Ivar|title=Object Oriented Software Engineering|year=1992|publisher=Addison-Wesley ACM Press|isbn=978-0-201-54435-0|pages=[https://archive.org/details/objectorientedso00jaco/page/43 43–69]|author2=Magnus Christerson|author3=Patrik Jonsson|author4=Gunnar Overgaard|url=https://archive.org/details/objectorientedso00jaco/page/43}}</ref>
Subclasses can override the methods defined by superclasses. [[Multiple inheritance]] is allowed in some languages, though this can make resolving overrides complicated. Some languages have special support for [[mixin]]s, though in any language with multiple inheritance, a mixin is simply a class that does not represent an is-a-type-of relationship. Mixins are typically used to add the same methods to multiple classes. For example, class UnicodeConversionMixin might provide a method unicode_to_ascii() when included in class FileReader and class WebPageScraper, which don't share a common parent.
[[Abstract class]]es cannot be instantiated into objects; they exist only for the purpose of inheritance into other "concrete" classes that can be instantiated. In Java, the <code>[[final (Java)|final]]</code> keyword can be used to prevent a class from being subclassed.
The doctrine of [[composition over inheritance]] advocates implementing has-a relationships using composition instead of inheritance. For example, instead of inheriting from class Person, class Employee could give each Employee object an internal Person object, which it then has the opportunity to hide from external code even if class Person has many public attributes or methods. Some languages, like [[Go (programming language)|Go]] do not support inheritance at all.
The "[[open/closed principle]]" advocates that classes and functions "should be open for extension, but closed for modification".
===Polymorphism===
[[Subtyping]] – a form of [[polymorphism (computer science)|polymorphism]] – is when calling code can be independent of which class in the supported hierarchy it is operating on – the parent class or one of its descendants. Meanwhile, the same operation name among objects in an inheritance hierarchy may behave differently.
For example, objects of type Circle and Square are derived from a common class called Shape. The Draw function for each type of Shape implements what is necessary to draw itself while calling code can remain indifferent to the particular type of Shape being drawn.
This is another type of abstraction that simplifies code external to the class hierarchy and enables strong [[separation of concerns]].
===Open recursion===
In languages that support [[open recursion]], object methods can call other methods on the same object (including themselves), typically using a special variable or keyword called <code>this</code> or <code>self</code>. This variable is ''[[name binding|late-bound]]''; it allows a method defined in one class to invoke another method that is defined later, in some subclass thereof.
==OOP languages==
===OOP in a network protocol===
The messages that flow between computers to request services in a client-server environment can be designed as the linearizations of objects defined by class objects known to both the client and the server. For example, a simple linearized object would consist of a length field, a code point identifying the class, and a data value. A more complex example would be a command consisting of the length and code point of the command and values consisting of linearized objects representing the command's parameters. Each such command must be directed by the server to an object whose class (or superclass) recognizes the command and is able to provide the requested service. Clients and servers are best modeled as complex object-oriented structures. [[Distributed Data Management Architecture]] (DDM) took this approach and used class objects to define objects at four levels of a formal hierarchy:
* Fields defining the data values that form messages, such as their length, code point and data values.
* Objects and collections of objects similar to what would be found in a [[Smalltalk]] program for messages and parameters.
* A client or server consisting of all the managers necessary to implement a full processing environment, supporting such aspects as directory services, security and concurrency control.
The initial version of DDM defined distributed file services. It was later extended to be the foundation of [[DRDA|Distributed Relational Database Architecture]] (DRDA).
==Design patterns==
|