In object-oriented programming, a class consists of encapsulated instance variables and subprograms. Classes describe the rules by which objects behave; those objects, described by a particular class, are known as "instances" of said class. A class specifies the data which each instance contains; as well as the methods (functions) which the object can perform; such methods are sometimes described as "behavior".
Classes are sometimes described as "blueprints"; instances, of a class, will have certain aspects in common. One might describe a "class of animals" (dogs), or a "class of tangible objects" (computers). One of the benefits, of programming with classes, is that all instances of a particular class will follow the defined behaviour of said class. For example: if humans are a class; then each person is an instance of an object of the human class. Each person is generally alike; but varies in such properties as "height" and "weight". The class would list such instance variables; and also define, via subprograms, the actions which humans can perform: "marriage", sleeping, "tonsillectomy", etc.
Table of contents |
2 Reasons for Implementing Classes 3 Types of classes 4 Object-Based Programming 5 C++ Examples 6 See also |
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.
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 possibility 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.
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.
Subclasses and Superclasses
Reasons for Implementing Classes
Types of classes
Object-Based Programming
Some languages have objects, but no classes; In such "object-based languages", objects are not restricted to class structure.C++ Examples
The first example shows how to define a C++ class; the example has no date, and performs no functions. It only contains the comment, "this is a class". The second example is of a somewhat more complex class definition.Example 1
class example {
// this is a class
};