ENCYCLOPEDIA 4U .com



Encyclopedia Home Page

Google
  Web Encyclopedia4u.com

 

Class (computer science)

The neutrality of the title of this article is disputed.

The factual accuracy of this article is disputed: see talk

In object-oriented programming, a class describes the rules by which certain objects behave. The objects which are described by a particular class are called instances of that class. This is similar to the way that many houses can be constructed from the same architect's blueprint, or the way that despite many individual differences, all human beings have certain things in common by which they can be identified as human. One of the benefits of programming with classes is the knowledge that all instances of a particular class will follow the defined behaviour for that class, regardless of their differences.

A class specifies the data items each object of the class contains and the operations or methods that can be performed on each object belonging to the class (in object-oriented languages that lack generic functions).

For example, in the case of human beings each instance (person) has properties such as "height" and "weight" which vary between different objects. The class describing human beings would therefore list these among the data items of the class. It would also define the operations which can be performed on human beings ("tonsilectomy", perhaps, or "marriage", or any of the other behaviours of human beings which the programmer wishes to represent).

(Some languages have objects but no classes. In these languages, objects are not restricted to structure provided by classes, and can be changed at will. This less common technique is called object-based programming.)

Table of contents
1 Subclasses and Superclasses
2 Types of classes

Subclasses and Superclasses

Classes are often related in some way. The most popular of these relations is inheritance, which involves subclasses and superclasses, also known respectively as child classes (or derived classes) and parent classes (or base classes). If [car] was a class, then [Jaguar] and [Porsche] might be two sub-classes. If [Button] is a subclass of [Control], then all buttons are controls.

Some programming languages (for example C++) allow multiple inheritance - they allow a child class to have more than one parent class. This technique has been criticized by some for its unnecessary complexity and being hard to implement efficiently, though some projects have undoubtedly benefited from its use. Java, for example has no multiple inheritance, its designers feeling that this would be more trouble than it was worth.

Sub- and superclasses are considered to exist within a hierarchy.

Reasons for Implementing Classes

Classes, when used properly, can accelerate development by reducing redundant code entry, testing and bug fixing. If a class has been thoroughly tested and is known to be a solid work, it stands to reason that implementing that class or extending it will reduce if not eliminate the possiblity of bugs propagating into the code. In the case of extension new code is being added so it also requires the same level of testing before it can be considered solid.

Another reason for using classes is to simplify the relationships of interrelated data. A car, for instance, has many components. These components in turn are based on other smaller components. The car has an engine and it has gears for example. To try to capture the essence of a car by describing its 1000s of individual parts would take much time and effort. Instead, by encapsulating each part by its purpose or its placement in the car we can simplify the coding of a car. We would create a gear class and an engine class and finally a car class. Each would embed information about parts internal to itself and would manage this implicitly for us. This abstraction allows developers to concentrate on the task at hand and not the internals of every problem.

See also: hierarchy, object-oriented programming

For related meanings of the word "class", see Class.

Types of classes

An abstract class is one that is designed only as a parent class and from which child classes may be derived, and which is not itself suitable for instantiation. Abstract classes are often used to represent abstract concepts or entities. The incomplete features of the abstract class are then shared by a group of sibling sub-classes which add different variations of the missing pieces.

Abstract classes are superclasses which contain abstract methods and are defined such that subclasses are to extend them by implementing the methodss. The behaviors defined by such a class are "generic" and much of the class will be undefined and unimplemented. Before a class derived from an abstract class can be instantiated, it must implement particular methods for all the abstract methods of its parent classes.





Content on this web site is provided for informational purposes only. We accept no responsibility for any loss, injury or inconvenience sustained by any person resulting from information published on this site. We encourage you to verify any critical information with the relevant authorities.



Copyright © 2005 Par Web Solutions All Rights reserved.
| Privacy

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Class (computer science)".