Encyclopedia  |   World Factbook  |   World Flags  |   Reference Tables  |   List of Lists     
   Academic Disciplines  |   Historical Timeline  |   Themed Timelines  |   Biographies  |   How-Tos     
Sponsor by The Tattoo Collection
Class (computer science)
Main Page | See live article | Alphabetical index

Class (computer science)

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". A class is the most specific type of an object.

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 actionss which humans can perform: "run", "jump", "sleep", "throw object", etc.

Table of contents
1 Interface to a class
2 Structure of a class
3 Subclasses and Superclasses
4 Reasons for Implementing Classes
5 Types of classes
6 Metaclasses
7 Object-Based Programming
8 C++ Examples
9 Java Examples
10 See also

Interface to a class

A class can implement (or realize) one or more interfacess. Each interface specifies operation signatures of some of the methods of the class. The methods of a class can either be used via one of its interfaces or directly. An interface of a class describes the possible behaviours of objects of the class from user's point of view in a particular situation. Multiple classes can implement the same interface.

The object-oriented programming methodology is designed in such way that the operations of any interface of a class are usually chosen to be independent of each other. This means that an interface places no requirements for clients to invoke the operations of one interface in any particular order. This approach has the benefit that client code can rely that the operations of an interface are available for use whenever the client holds a valid reference to the object. This will also result in a client-server (or layered) design where servers do not depend in any way of the clients.

The methods that are not in any of the interfaces of a class are private to the class, and not intended to be called by other objects.

The "set of all interfaces of a class" is sometimes called the interface of the class.

The internal data structures defined as part of a class are not considered to be part of its interface. Rather, public accessor methods can be used to inspect or alter object data. The various object-oriented programming languages enforce this to various degrees. For example, Java does not allow the programmer to access the private data of a class at all, whereas in languages like Objective-C or Perl the programmer can do what they want. In C++, private methods are visible but not accessible in the interface; however they are commonly made invisible by explicitly declaring fully abstract classes that represent the interfaces of the class.

Structure of a class

A class contains a description of data ("state") stored in the objects of the class. The state of an object is stored in some resource, such as memory, database or a file. The storage is assumed to be located in a specific location, such that it is possible to create references that refer to that location.

A class implements its interfaces by specifying methods that describe what operations can be performed on the data stored in the objects of the class. Each method specifies only tasks that are related to the stored data. Multi-methods can be used when a single task requires access to many objects' data.

A class also describes a set of invariants that are preserved by every method in the class. An invariant is a constraint on the state of an object that should be satisfied by every object of the class. The main purpose of the invariants is to establish what objects belong to the class. An invariant is what distinguishes datatypes and classes from each other, that is, a class does not allow use of all possible values for the state of the object, only those that are well-defined by the semantics of the intended use of the datatype. The set of supported methods often implicitly establishes an invariant. Some programming languages support specification of invariants as part of the definition of the class, and enforce them via the type system.

A class specifies constructor and destructor functions that allow creation and destruction of objects of the class. A constructor that takes arguments can be used to create an object from data. A destructor that returns a value can be used to obtain a representation of an object of a class. The main purpose of a constructor is to establish the invariant of the class, failing if the invariant isn't valid. The main purpose of a destructor is to destroy the identity of the object, invalidating any references in the process. Constructors and destructors are also sometimes used to reserve and release resources associated with the object.

A class (or sometimes the programming language that supports classes) specifies mechanisms for manipulating references to objects of the class. A class, together with its methods, specifies the lifetime of references to objects of the class. A class specifies a representation for the reference via the structure of the identity of the objects of the class. A class specifies mechanism for finding the object based on an object reference.

A class can also implement a set of auxiliary functions, sometimes called class functions or static methods. Static methods are often used to find, create or destroy objects of the class. Constructors and destructors are sometimes specified as static methods. Often, mechanisms for sending an object to another location or changing the class of an object are specified as static methods.

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.

Conceptually, a superclass should be considered as a common part of its subclasses. This factoring of commonality is one mechanism for providing reuse. Thus, extending a superclass by modifying the existing class is also likely to narrow its applicability in various situations. In Object-oriented design, careful balance between applicability and functionality of superclasses should be considered.

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 difficult to implement efficiently, though some projects have certainly 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 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. Rather than writing code to repeatedly draw a GUI window on the terminal screen, it is simpler to represent the window as an object and tell it to draw itself as necessary. With classes, GUI items that are similar to windows (such as dialog boxes) can simply inherit most of their functionality and data structures from the window class. The programmer then need only add code to the dialog class that is unique to its operation. Indeed, GUIs are a very common and useful application of classes, and GUI programming is generally much easier with a good class framework.

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. In C++, an abstract class is defined as a class having at least one virtual function without an implementation.

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.

Metaclasses

Metaclasses are classes whose instances are classes. A metaclass describes a common structure of a collection of classes. A metaclass can implement a design pattern or describe a shorthand for particular kinds of classes. Metaclasses are often used to describe frameworks.

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 data, 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
};

Example 2

  1. include
using std::string;

class InetMessage {

 string m_subject, m_to, m_from;

public:
 InetMessage (const string& subject,
const string& to, const string& from);
 string subject () const;
 string to () const;
 string from () const;
};

Java Examples

The first example shows the simplest Java class possible. The second example shows a slightly more complex class that has a defined constructor, one member data, and two accessor methods for that member data. It extends the first example's class. Note that in Java all classes automatically extend the class Object. This allows you to write generic code to deal with objects of any type.

Example 1

public class Example1
{
 // This is a Java class, it automatically extends the class Object
}

Example 2

public class Example2 extends Example1
{
 // This is a class that extends the class created in Example 1.
 protected int data;

public Example2() { // This is a constructor for the class. It does not have a return type. data = 1; }

public int getData() { return data; }

public void setData(int d) { data = d; }

}

See also