Content deleted Content added
Stevebroshar (talk | contribs) Move interesting stuff to the intro |
Stevebroshar (talk | contribs) →Inheritance: Edit for brevity |
||
Line 133:
===Inheritance===
==== Class-based ====
In [[class-based programming]], the most common type of OOP,
* [[Class variable]]
* [[Instance variable]]
* [[Member variable]]
* Instance
Classes may inherit from other classes, creating a hierarchy of
▲* [[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.
==== Prototype-based ====
▲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
▲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====
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]].
|