Object-oriented programming: Difference between revisions

Content deleted Content added
Features: we don't need to list stuff that isn't OOP in the OOP article
Objects: Composition is a feature
Line 110:
The OOP features provided by languages varies. Below are some common features of OOP languages.<ref name="ArmstrongQuarks">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.</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> Comparing OOP with other styles, like [[relational programming]], is difficult because there isn't a clear, agreed-upon definition of OOP.<ref name="DatePage650">C. J. Date, Introduction to Database Systems, 6th-ed., Page 650</ref>
 
===ObjectsObject===
An object consists of [[Field (computer science)| fields]] and [[Method (computer programming)| method]]s. A field (a.k.a. attribute or property) holds information (a.k.a. state) as a [[variable (computer science)| variable]]. A method (a.k.a. [[function (programming)| function]] or action) defines behavior via logic code. Objects are usually stored in [[Memory address| memory]].
{{Main|Object (computer science)}}
 
An object is a type of [[data structure]] that has two main parts: [[Field (computer science)|fields]] and [[Method (computer programming)|method]]s. Fields may also be known as members, attributes, or properties, and hold information in the form of state [[variable (computer science)|variable]]s. Methods are actions, [[subroutine]]s, or procedures, defining the object's behavior in code. Objects are usually stored in [[Memory address|memory]], and in many programming languages, they work like [[Pointer (computer programming)|pointers]] that link directly to a contiguous block containing the object instances's data.
 
Objects can contain other objects. This is called [[object composition]]. For example, an Employee object might have an Address object inside it, along with other information like "first_name" and "position". This type of structures shows "has-a" relationships, like "an employee has an address".
 
Some believe that OOP places too much focus on using objects rather than on [[algorithm]]s and [[data structure]]s.<ref name="stepanov"/><ref name="hickey"/> For example, programmer [[Rob Pike]] pointed out that OOP can make programmers think more about type hierarchy than composition.<ref name="RobPike">{{cite web |url=https://commandcenter.blogspot.com/2012/06/less-is-exponentially-more.html |title=Less is exponentially more |last1=Pike |first1=Rob |date=25 June 2012 |access-date=1 October 2016 }}</ref> He has called object-oriented programming "the [[Roman numerals]] of computing".<ref>{{cite mailing list |last1=Pike |first1=Rob |author1-link=Rob Pike |date=2 March 2004 |url=http://groups.google.com/group/comp.os.plan9/msg/006fec195aeeff15 |title=[9fans] Re: Threads: Sewing badges of honor onto a Kernel |access-date=17 November 2016 |mailing-list=comp.os.plan9}}</ref> [[Rich Hickey]], creator of [[Clojure]], described OOP as overly simplistic, especially when it comes to representing real-world things that change over time.<ref name="hickey">Rich Hickey, JVM Languages Summit 2009 keynote, [http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey Are We There Yet?] November 2009.</ref> [[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 in [[generic programming]]. Stepanov also said that calling everything an "object" doesn't add much understanding.<ref name="stepanov">{{Cite web| url=http://www.stlport.org/resources/StepanovUSA.html| title=STLport: An Interview with A. Stepanov| last=Stepanov| first=Alexander| access-date=21 April 2010| author-link=Alexander Stepanov}}</ref>
 
====Real-world modeling and relationships====
Line 136 ⟶ 130:
 
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. [[Robert C. Martin|Bob Martin]] suggests that because classes are software, their relationships don't match the real-world relationships they represent.<ref>{{cite web |url=https://www.youtube.com/watch?v=zHiWqnTWsn4 |title=Uncle Bob SOLID principles |website=[[YouTube]] |date=2 August 2018}}</ref> [[Bertrand Meyer]] argues in ''[[Object-Oriented Software Construction]]'', 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".{{sfn|Meyer|1997|p=230}} [[Steve Yegge]] noted that natural languages lack the OOP approach of strictly prioritizing ''things'' (objects/[[noun]]s) before ''actions'' (methods/[[verb]]s), as opposed to [[functional programming]] which does the reverse.<ref name="executioniKoN">{{Cite web| first = Steve| last=Yegge |title = Execution in the Kingdom of Nouns| date=30 March 2006|access-date=3 July 2010| publisher = steve-yegge.blogspot.com| url=http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html}}</ref> This can sometimes make OOP solutions more complicated than those written in procedural programming.<ref name="executioniKoN2">{{Cite web| first = Timothy| last= Boronczyk |title = What's Wrong with OOP| date=11 June 2009|access-date=3 July 2010| publisher = zaemis.blogspot.com| url=http://zaemis.blogspot.com/2009/06/whats-wrong-with-oop.html}}</ref>
 
===Composition===
ObjectsVia [[object composition]], an object can contain other objects. This is called [[object composition]]. For example, an {{code|Employee}} object might havecontain an {{code|Address}} object inside it, along with other information like "first_name"{{code|name}} and "{{code|position"}}. ThisComposition type of structuresis showsa "has-a" relationships, like "an employee has an address".
 
===Inheritance===