Content deleted Content added
m Establishing abbreviation. |
change paradigms template to navbox (see Template talk:Programming paradigms#too long) |
||
(35 intermediate revisions by 28 users not shown) | |||
Line 1:
{{Short description|Style of object-oriented programming where inheritance is based on classes}}
{{Refimprove|date=February 2013}}
{{Programming paradigms}}▼
'''Class-based programming''', or more commonly '''class-orientation''', is a style of [[object-oriented programming]] (OOP) in which [[inheritance
The most popular and developed model of OOP is a class-based model,
==Encapsulation==
[[Information hiding|Encapsulation]] prevents users from breaking the [[Invariant (computer science)|invariants]] of the class, which is useful because it allows the implementation of a class of objects to be changed for aspects not exposed in the interface without impact to user code. The definitions of encapsulation focus on the grouping and packaging of related information ([[cohesion (computer science)|cohesion]]) rather than security issues
==Inheritance==
In class-based programming, [[inheritance (object-oriented programming)|inheritance]] is done by defining new classes as [[Extension (semantics)|extensions]] of existing classes: the existing class is the [[parent class]] and the new class is the [[Subclass (computer science)|child class]]. If a child class has only one parent class, this is known as ''[[single inheritance]],'' while if a child class can have more than one parent class, this is known as ''[[multiple inheritance]].'' This organizes classes into a [[hierarchy]], either a ''[[Tree (set theory)|tree]]'' (if single inheritance) or ''[[Lattice (order)|lattice]]'' (if multiple inheritance).
The defining feature of inheritance is that both interface and implementation are inherited; if only interface is inherited, this is known as [[interface inheritance]] or ''subtyping.'' Inheritance can also be done without classes, as in [[prototype-based programming]].
Class-based languages, or, to be more precise, [[typed language]]s, where [[Subclass (computer science)|subclassing]] is the only way of [[subtyping]], have been criticized for mixing up implementations and interfaces—the essential principle in object-oriented programming. The critics say one might create a bag class that stores a [[Collection class|collection]] of objects, then extends it to make a new class called a set class where the duplication of objects is eliminated.<ref>{{cite web|first=Oleg|last=Kiselyov|title=Subtyping, Subclassing, and Trouble with OOP|url=http://okmij.org/ftp/Computation/Subtyping/|accessdate=7 October 2012}}</ref><ref>{{cite web|title=A set cannot be a subtype of a bag|last=Ducasse|first=Stéphane|url=http://stephane.ducasse.free.fr/Resources/LecturesInPowerpoint/STOOP-416-LSP.ppt|accessdate=7 October 2012}}</ref> Now, a function that takes a bag class may expect that adding two objects increases the size of a bag by two, yet if one passes an object of a set class, then adding two objects may or may not increase the size of a bag by two. The problem arises precisely because subclassing implies subtyping even in the instances where the principle of subtyping, known as the [[Liskov substitution principle]], does not hold. Therefore normally one must distinguish subtyping and subclassing. Most current object-oriented languages distinguish subtyping and subclassing, however some approaches to design do not.▼
==Critique==
Also, another common example is that a person object created from a [[child class]] cannot become an object of [[parent class]] because a child class and a parent class inherit a person class but class-based languages mostly do not allow to change the kind of class of the object at runtime. For class-based languages, this restriction is essential in order to preserve unified view of the class to its users. The users should not need to care whether one of the implementations of a method happens to cause changes that break the [[Invariant (computer science)|invariants]] of the class. Such changes can be made by destroying the object and constructing another in its place. Polymorphism can be used to preserve the relevant interfaces even when such changes are done, because the objects are viewed as black box abstractions and accessed via object [[identity (object-oriented programming)|identity]]. However, usually the value of object references referring to the object is changed, which causes effects to client code.▼
▲Class-based languages, or, to be more precise, [[typed language]]s, where [[Subclass (computer science)|subclassing]] is the only way of [[subtyping]], have been criticized for mixing up implementations and interfaces—the essential principle in object-oriented programming. The critics say one might create a bag class that stores a [[Collection class|collection]] of objects, then
<!-- inline math as LaTeX has poor baseline alignment (better with MathJaX HTML/CSS rendering of LaTeX)
:''Let <math>q(x)</math> be a property provable about objects <math>x</math> of type <math>T</math>. Then <math>q(y)</math> should be provable for objects <math>y</math> of type <math>S</math>, where <math>S</math> is a subtype of <math>T</math>.''
--><!-- inline math as HTML -->
<blockquote>''Subtype Requirement'': Let {{tmath| \phi(x) }} be a property provable about objects {{tmath| x }} of type {{tmath| T}}. Then {{tmath| \phi(y) }} should be true for objects {{tmath| y }} of type {{tmath| S }} where {{tmath| S }} is a subtype of {{tmath| T}}. </blockquote>
<!-- inline math as HTML bold
:''Let {{math| '''q(x)''' }} be a property provable about objects {{math| '''x''' }} of type {{math| '''T'''. }} Then {{math| '''q(y)''' }} should be provable for objects {{math| '''y''' }} of type {{math| '''S''', }} where {{math| '''S''' }} is a subtype of {{math| '''T'''. }}''
-->
Thus, normally one must distinguish subtyping and subclassing. Most current object-oriented languages distinguish subtyping and subclassing, however some approaches to design do not.
▲Also, another common example is that a person object created from a [[Subclass (computer science)|child class]] cannot become an object of [[parent class]] because a child class and a parent class inherit a person class but class-based languages mostly do not allow to change the kind of class of the object at runtime. For class-based languages, this restriction is essential in order to preserve unified view of the class to its users. The users should not need to care whether one of the implementations of a method happens to cause changes that break the [[Invariant (computer science)|invariants]] of the class. Such changes can be made by destroying the object and constructing another in its place. Polymorphism can be used to preserve the relevant interfaces even when such changes are done, because the objects are viewed as black box abstractions and accessed via object [[identity (object-oriented programming)|identity]]. However, usually the value of object references referring to the object is changed, which causes effects to client code.
==Example languages==
{{Category see also|Class-based programming languages}}
Although [[Simula]] introduced the class abstraction, the canonical example of a class-based language is [[Smalltalk]]. Others include [[PHP]], [[C++]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]], and [[Objective-C]].
==See also==▼
*[[Prototype-based programming]] (contrast)
*[[Programming paradigm]]s▼
==References==
{{Reflist}}
▲{{Programming paradigms navbox}}
▲==See also==
▲*[[Programming paradigm]]s
▲*[[class (computer science)]]
{{DEFAULTSORT:Class-Based Programming}}
|