Class-based programming

Source: Wikipedia, the free encyclopedia.

Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance occurs via defining classes of objects, instead of inheritance occurring via the objects alone (compare prototype-based programming).

The most popular and developed model of OOP is a class-based model, instead of an object-based model. In this model, objects are entities that combine

instance of that class. An object is similar to a structure
, with the addition of method pointers, member access control, and an implicit data member which locates instances of the class (i.e., objects of the class) in the class hierarchy (essential for runtime inheritance features).

Encapsulation

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
) rather than security issues.

Inheritance

In class-based programming,

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 (if single inheritance) or 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
.

Critique

Class-based languages, or, to be more precise,

collection of objects, then extend it to make a new class called a set class where the duplication of objects is eliminated.[1][2] Now, a function that takes an object of the 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. Barbara Liskov and Jeannette Wing
formulated the principle succinctly in a 1994 paper as follows:

Subtype Requirement: Let be a property provable about objects of type . Then should be true for objects of type where is a subtype of .

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

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
. However, usually the value of object references referring to the object is changed, which causes effects to client code.

Example languages

Although Simula introduced the class abstraction, the canonical example of a class-based language is Smalltalk. Others include PHP, C++, Java, C#, and Objective-C.

See also

References

  1. ^ Kiselyov, Oleg. "Subtyping, Subclassing, and Trouble with OOP". Retrieved 7 October 2012.
  2. ^ Ducasse, Stéphane. "A set cannot be a subtype of a bag". Retrieved 7 October 2012.