Object-oriented programming: Difference between revisions

Content deleted Content added
See also: better link
typo
 
(3 intermediate revisions by 3 users not shown)
Line 135:
Some programming languages, like Java, provide information hiding via visibility key words ({{code|private}} and {{code|public}}).{{sfn|Bloch|2018|loc=Chapter §4 Item15 Minimize the accessibility of classes and members|pp=73-77}} Some languages 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's {{code|protected}} keyword, (which allows access from the same class and its subclasses, but not objects of a different class), and the {{code|internal}} keyword in C#, Swift, and Kotlin, which restricts access to files within the same module.<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}}{{Dead link|date=July 2025 |bot=InternetArchiveBot |fix-attempted=yes}}</ref>
 
Supporters of information hiding and data abstraction say it makes code easier to reuse and intuitively represents real-world situations.<ref name="Luca1985">{{Cite journal |last1=Cardelli |first1=Luca |last2=Wegner |first2=Peter |date=1985-12-10 |title=On understanding types, data abstraction, and polymorphism |journal=ACM Computing Surveys |language=en |volume=17 |issue=4 |pages=471–523 |doi=10.1145/6041.6042 |issn=0360-0300|doi-access=free}}</ref> <ref>{{cite book|last1=Jacobsen|first1=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> However, others argue that OOP does not enhance readability or modularity.<ref name="badprop"/><ref name="armstrongjoe"/> [[Eric S. Raymond]] has written that OOP languages tend to encourage thickly layered programs that destroy transparency.<ref name="Eric S. Raymond 2003">{{cite web|url=http://www.catb.org/esr/writings/taoup/html/unix_and_oo.html|title=The Art of Unix Programming: Unix and Object-Oriented Languages|last1=Raymond|first1=Eric S.|date=2003|access-date=6 August 2014}}</ref> Raymond compares this unfavourably to the approach taken with [[Unix]] and the [[C (programming language)|C]] language.<ref name="Eric S. Raymond 2003"/>
 
[[SOLID]] includes the [[open/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.<ref name="badprop"/> The latter point is reiterated by [[Joe Armstrong (programmer)|Joe Armstrong]], the principal inventor of [[Erlang (programming language)|Erlang]], who is quoted as saying:<ref name="armstrongjoe">{{cite book |last1=Armstrong |first1=Joe |author1-link=Joe Armstrong (programmer) |editor1-last=Seibel |editor1-first=Peter |title=Coders at Work: Reflections on the Craft of Programming |url=http://www.codersatwork.com/ |publisher=Codersatwork.com |archive-url=https://web.archive.org/web/20100305165150/http://www.codersatwork.com/ |archive-date=5 March 2010 |access-date=13 November 2009}}</ref>
Line 141:
{{Blockquote|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.}}
 
Leo Brodie says that information hiding can lead to [[duplicate code]],<ref>{{Cite book |last1=Brodie |first1=Leo |year=1984 |url=httphttps://thinking-forth.sourceforge.net/thinking-forth-ans.pdf |title=Thinking Forth |pages=92–93 |access-date=4 May 2018}}</ref> which goes against the [[don't repeat yourself]] rule of software development.<ref>{{cite web |work=Category Extreme Programming |last1=Hunt |first1=Andrew |url=http://wiki.c2.com/?DontRepeatYourself |title=Don't Repeat Yourself |access-date=4 May 2018}}</ref>
 
===Composition===
Line 181:
 
===Dynamic dispatch===
A method mymay be invoked via [[dynamic 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 called [[multiple dispatch]].
 
Dynamic dispatch works together with inheritance: if an object doesn't have the requested method, it looks up to its parent class ([[Delegation (object-oriented programming)|delegation]]), and continues up the chain to find a matching method.
Line 261:
 
==Formal semantics==
Researchers have tried to formally define the [[Semantics (computer science)|semantics]] of OOP. [[Inheritance (object-oriented programming)|inheritanceInheritance]] presents difficulties, particularly with the interactions between open recursion and encapsulated state. Researchers have used [[recursive type]]s and [[F-Coalgebra|co-algebraic data types]] to incorporate essential features of OOP.<ref name=poll97>{{cite web|last1=Poll|first1=Erik|title=Subtyping and Inheritance for Categorical Datatypes|url=https://www.cs.ru.nl/E.Poll/papers/kyoto97.pdf|access-date=5 June 2011}}</ref> Abadi and Cardelli defined several extensions of [[System F-sub|System F<sub><:</sub>]] that deal with mutable objects, allowing both [[subtype polymorphism]] and [[parametric polymorphism]] (generics), and were able to formally model many OOP concepts and constructs.<ref name="AbadiCardelli">{{Cite book |first1=Martin |last1=Abadi |title=A Theory of Objects |url=http://portal.acm.org/citation.cfm?id=547964&dl=ACM&coll=portal |year=1996 |access-date=21 April 2010 |isbn=978-0-387-94775-4 |publisher=Springer-Verlag New York, Inc. |author-link=Martin Abadi|author2=Cardelli, Luca}}</ref> Although far from trivial, static analysis of object-oriented programming languages such as Java is a mature field,<ref>{{cite conference |last1=Tan |first1=Tian |last2=Li |first2=Yue |date=12 July 2023 |title=Tai-e: A Developer-Friendly Static Analysis Framework for Java by Harnessing the Good Designs of Classics|conference=ISSTA 2023 |pages=1093–1105 |doi=10.1145/3597926.3598120}}</ref> with several commercial tools.<ref>{{cite journal |last1=Bhutani |first1=Vikram |last2=Toosi |first2=Farshad Ghassemi |last3=Buckley |first3=Jim |date=1 June 2024 |title=Analysing the Analysers: An Investigation of Source Code Analysis Tools |journal=Applied Computer Systems |volume=29 |issue=1 |pages=98–111 |doi=10.2478/acss-2024-0013}}</ref>
 
=={{anchor|Criticism}}Criticism==