Object-oriented programming: Difference between revisions

Content deleted Content added
Move interesting stuff to the intro
Inheritance: Edit for brevity
Line 133:
 
===Inheritance===
Most OOP languages allow [[code reuse|reusing]] and [[extensibility|extending]] code through "[[Inheritance (object-oriented programming)|inheritanceInheritance]]". Thisbe inheritancesupported can usevia eitherthe "[[Class (computer programming)|classesclass]]" or "the [[Prototype-based programming|prototypes prototype]]", which have some differences but use similar terms for ideas like "object" and "[[instance" (computer science)|instance]].
 
==== Class-based ====
 
In [[class-based programming]], the most common type of OOP, everyan object is an [[instance (computer science)|instance]] of a specific ''class''. The class defines the data format, like (variables (e.g., name, age) and methods (actions the object can takelogic). EveryAn instanceobject of the class has the same set of variables and methods. Objects areis created using a special method invia the class known as a [[Constructor (object-oriented programming)|constructor]]. Elements of class may include:
 
* [[Class variable]]sbelongbelongs to the ''class itself'', so; all objects inof the class share one copy.
Here are a few key terms in class-based OOP:
* [[Instance variable]]sbelongbelongs to individualan ''objects''object; every object has its own version of these variables.
* [[Member variable]]s – refers to both the class and instance variables that are defined byof a particular class.
* Class methodsmethod linked to the ''class itself'' and can only use class variables.
* Instance methodsmethodbelongbelongs to ''individualan objects'', and; can use both instance and class variables
 
Classes may inherit from other classes, creating a hierarchy of "subclasses"classes -- a subclass inheriting from a super-class. For example, an "{{code|Employee"}} class might inherit from a "{{code|Person"}} class. Thiswhich meansendows the Employee object will have allwith the variables from {{code|Person}}. (likeThe namesubclass variables)may plus any newadd variables (like job position and salary).methods Similarly,that thedo subclassnot may expandaffect the interface with new methodssuper-class. Most languages also allow the subclass to ''override'' thesuper-class methods defined by superclasses. Some languages support [[multiple inheritance]], where a class can inherit from more than one class, and other languages similarly support [[mixin]]s or [[Trait (computer programming)|traits]]. For example, a mixin called UnicodeConversionMixin might add a method unicode_to_ascii() to both a FileReader and a WebPageScraper class.
* [[Class variable]]s – belong to the ''class itself'', so all objects in the class share one copy.
* [[Instance variable]]s – belong to individual ''objects''; every object has its own version of these variables.
* [[Member variable]]s – refers to both the class and instance variables that are defined by a particular class.
* Class methods – linked to the ''class itself'' and can only use class variables.
* Instance methods – belong to ''individual objects'', and can use both instance and class variables
 
An [[abstract class]] cannot be directly instantiated as an object. It is only used as a super-class.
Classes may inherit from other classes, creating a hierarchy of "subclasses". For example, an "Employee" class might inherit from a "Person" class. This means the Employee object will have all the variables from Person (like name variables) plus any new variables (like job position and salary). Similarly, the subclass may expand the interface with new methods. Most languages also allow the subclass to ''override'' the methods defined by superclasses. Some languages support [[multiple inheritance]], where a class can inherit from more than one class, and other languages similarly support [[mixin]]s or [[Trait (computer programming)|traits]]. For example, a mixin called UnicodeConversionMixin might add a method unicode_to_ascii() to both a FileReader and a WebPageScraper class.
 
Some classes are [[Abstract class|abstract]], meaning they cannot be directly instantiated into objects; they're only meant to be inherited into other classes. Other classes are ''utility'' classes which contain only class variables and methods and are not meant to be instantiated or subclassed.{{sfn|Bloch|2018|loc=Chapter §2 Item 4 Enforce noninstantiability with a private constructor|p=19}}
 
==== Prototype-based ====
InInstead of providing a class concept, in [[prototype-based programming]], there aren't any classes. Instead, eachan object is linked to another object, called its ''prototype'' or ''parent''. In Self, an object may have multiple or no parents,<ref>{{cite book |chapter= Classifying prototype-based programming languages|chapter-url=https://www.lirmm.fr/~dony/postscript/proto-book.pdf|first1=C|last1=Dony|first2=J|last2=Malenfant|first3=D|last3=Bardon|title=Prototype-based programming: concepts, languages and applications |date=1999 |publisher=Springer |___location=Singapore Berlin Heidelberg |isbn=9789814021258}}</ref> but in the most popular prototype-based language, [[Javascript]], everyan object has exactly one ''prototype'' link, up to the base Object typeobject whose prototype is null.
 
TheA prototype acts as a model for new objects. For example, if you have an object ''{{code|fruit''}}, you can make two objects ''{{code|apple''}} and ''{{code|orange'', based on it. There is no ''fruit'' class, but}} theythat share traits fromof the ''{{code|fruit''}} prototype. Prototype-based languages also allow objects to have their own unique properties, so the ''{{code|apple''}} object might have an attribute ''{{code|sugar_content''}}, while the ''{{code|orange''}} or ''{{code|fruit''}} objects do not.
In [[prototype-based programming]], there aren't any classes. Instead, each object is linked to another object, called its ''prototype'' or ''parent''. In Self, an object may have multiple or no parents,<ref>{{cite book |chapter= Classifying prototype-based programming languages|chapter-url=https://www.lirmm.fr/~dony/postscript/proto-book.pdf|first1=C|last1=Dony|first2=J|last2=Malenfant|first3=D|last3=Bardon|title=Prototype-based programming: concepts, languages and applications |date=1999 |publisher=Springer |___location=Singapore Berlin Heidelberg |isbn=9789814021258}}</ref> but in the most popular prototype-based language, Javascript, every object has exactly one ''prototype'' link, up to the base Object type whose prototype is null.
 
The prototype acts as a model for new objects. For example, if you have an object ''fruit'', you can make two objects ''apple'' and ''orange'', based on it. There is no ''fruit'' class, but they share traits from the ''fruit'' prototype. Prototype-based languages also allow objects to have their own unique properties, so the ''apple'' object might have an attribute ''sugar_content'', while the ''orange'' or ''fruit'' objects do not.
 
==== No inheritance ====
Some languages, like [[Go (programming language)|Go]], don't usesupport inheritance at all.<ref>{{Cite web |url=https://golang.org/doc/faq#Is_Go_an_object-oriented_language |title=Is Go an object-oriented language? |access-date=April 13, 2019 |quote=Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy.}}</ref> 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 (object-oriented programming)|Delegation]] is another language feature that can be used as an alternative to inheritance.
 
Some languages, like [[Go (programming language)|Go]], don't use inheritance at all.<ref>{{Cite web |url=https://golang.org/doc/faq#Is_Go_an_object-oriented_language |title=Is Go an object-oriented language? |access-date=April 13, 2019 |quote=Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy.}}</ref> 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 (object-oriented programming)|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.<ref>{{cite conference |last1=Stroustrup |first1=Bjarne |author1-link=Bjarne Stroustrup |title=Object-Oriented Programming without Inheritance (Invited Talk) |date=2015 |doi=10.4230/LIPIcs.ECOOP.2015.1 |doi-access=free |url=https://www.youtube.com/watch?v=xcpSLRpOMJM |conference=29th European Conference on Object-Oriented Programming (ECOOP 2015) |at=1:34}}</ref> [[Rob Pike]] has criticized inheritance for creating complicated hierarchies instead of simpler solutions.<ref>{{cite web |url=http://plus.google.com/+RobPikeTheHuman/posts/hoJdanihKwb |title=A few years ago I saw this page |last1=Pike |first1=Rob |access-date=1 October 2016 |date=14 November 2012|archive-url=https://web.archive.org/web/20180814173134/http://plus.google.com/+RobPikeTheHuman/posts/hoJdanihKwb |archive-date=14 August 2018}}</ref>
 
====Inheritance and behavioral subtyping====
{{See also|Object-oriented design}}<!-- not "further" because that article is mostly blather and does not even mention this -->
 
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 the [[program semantics]] are that objects from the subclass can always replace objects from the original class without problems. This concept is known as [[behavioral subtyping]], more specifically the [[Liskov substitution principle]].