FACTOID # 114: People in Germany, Belgium, Hungary and Sweden have to pay almost half their salaries in tax.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

Encyclopedia > Class (computer science)

In object-oriented programming, a class is a programming language construct used to group related fields and methods. It is conceptually related to classes in set theory. Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. ... A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ... In computer science, data that has several parts can be divided into fields. ... In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ... In set theory and its applications throughout mathematics, a class is a collection of sets (or sometimes other mathematical objects) that can be unambiguously defined by a property that all its members share. ... Set theory is the mathematical theory of sets, which represent collections of abstract objects. ...


A class is a cohesive package that consists of a particular kind of metadata. It describes the rules by which objects behave; these objects are referred to as instances of that class. A class specifies the structure of data, partitioned into fields (a.k.a. properties, attributes, data members), within each instance as well as method functions which interact with that data and provide behavior associated with the instance. A class is the most specific type of an object in relation to a specific layer. A class may also have a representation (metaobject) at runtime, which provides runtime support for manipulating the class-related metadata. In computer programming, cohesion is a measure of how strongly-related and focused the various responsibilities of a software module are. ... Metadata is data about data. ... An object is fundamental concept in object-oriented programming. ... Instantiation is the process of creating a specific object (computing) which is a member or instance of a class (computing). ... In Computer Science, data is often distinguished from code, though both are represented in modern computers as binary strings. ... In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ... This article does not cite any references or sources. ... In computer science, a datatype or data type (often simply a type) is a name or label for a set of values and some operations which one can perform on that set of values. ... In object-oriented design, a layer is a group of classes that have the same set of link-time module dependencies to other modules. ... In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. ...


Programming languages that support classes all subtly differ in their support for various class-related features. Most support various forms of class inheritance. Many languages also support features providing encapsulation, such as access specifiers. This article or section does not cite any references or sources. ... In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. ...

Contents

Reasons for using 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 using that well tested 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.


Interfaces and methods

Note: the term "interface" here isn't referring to a Java interface, although the two are closely related. An interface defines the communication boundary between two entities, such as a piece of software, a hardware device, or a user. ... In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ... An interface in the Java programming language is an abstract type which is used to specify an interface (in the generic sense of the term) that classes must implement. ...


Objects define their interaction with the outside world through the methods that they expose. A method is a function with a special property that it has access to data stored in an object. Methods that manipulate the data of the object and perform tasks are sometimes described as behavior. In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ... This article does not cite any references or sources. ...


Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off. In its most common form, an interface is a specification of a group of related methods without any associated implementation of the methods.


There is a distinction between an interface and the interface's implementation (or realization), which is the code that defines the class's structure and how the methods work (or alternatively, every part of the code related to the class that does more than define the interface of that class). In most languages, this line usually blurred, because a class declaration both defines an interface, and implements that interface by providing code for it. Some languages, however, provide features that help separate interface and implementation. For example, an abstract class can define an interface without providing implementation, and in Dylan, classes do not even define interfaces. Dylan is a dynamic programming language created by a group led by Apple Computer. ...


Languages that support class inheritance also allow classes to inherit interfaces from the classes that they are derived from. In languages that support access specifiers, the interface of a class is considered to be the set of public members of the class, including both methods and fields (via implicit getter and setter methods); any private members or internal data structures are not intended to be depended on by client code and thus are not part of the interface. In computer science the mutator method is a method that changes the value of the argument variables. ...


The object-oriented programming methodology is designed in such a 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 assume 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 on the clients. Client/Server is a network application architecture which separates the client (usually the graphical user interface) from the server. ...


Instantiation

A class is used to create new objects by instantiating them. Instantiation is the process of creating a specific object (computing) which is a member or instance of a class (computing). ...


Instances of a class share the same set of properties, yet may differ in what those properties contain. For example, a class "Person" would describe the properties common to all instances of the Person class. Each person is generally alike, but varies in such properties as "height" and "weight". The class would list types of such properties and also define, via methods, the actions which a person can perform: "run", "jump", "sleep", "throw object", etc. One of the benefits of programming with classes is that all instances of a particular class will follow the defined behavior of the class they instantiate.


In most languages, the structures as defined by the class determine how the memory used by its instances will be laid out. This technique is known as the cookie-cutter model. The alternative to the cookie-cutter model is that of for instance Python, where objects are structured as associative key-value containers. In such models, objects that are instances of the same class could contain different instance variables, as state can be dynamically added to the object. This may resemble prototype-based languages in some ways, but it is not equivalent. Python is a high-level programming language first released by Guido van Rossum in 1991. ... Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is accomplished by cloning existing objects which serve as prototypes for the new ones. ...


Structure of a class

UML notation for classes
UML notation for classes

A class contains a description of structure of data stored in the objects of the class. The state of an object's data is stored in some resource, such as memory or a file. The storage is assumed to be located in a specific location, such that it is possible to access the object through references to the identity of the objects. However, the actual storage location associated with an object may change with time. In such situations, the identity of the object does not change. The state is encapsulated and every access to the state occurs through methods of the class. Specific data items in the state, such as xsize and ysize in the example, are sometimes called class attributes or class properties. UML Class example. ... In the field of software engineering, the Unified Modeling Language (UML) is a standardized specification language for object modeling. ... In Computer Science, data is often distinguished from code, though both are represented in modern computers as binary strings. ... In information processing, a state is the complete set of properties (for example, its energy level, etc. ... This article is about a general notion of reference in computing. ... An identity in object-oriented programming, object-oriented design and object-oriented analysis describes the property of objects that the object can be distinguished from other objects. ...


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. Multimethods can be used when a single task requires access to many objects' data. Multiple dispatch or multimethods is the feature of some object-oriented programming languages in which a function or method can be specialized on the type of more than one of its arguments. ...


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 data types and classes from each other, that is, a class does not allow use of all possible values for the state of the object, and instead allows only those values that are well-defined by the semantics of the intended use of the data type. The set of supported (public) methods often implicitly establishes an invariant. Some programming languages support specification of invariants as part of the definition of the class, and enforce them through the type system. Encapsulation of state is necessary for being able to enforce the invariants of the class. In computer science, optimising compilers and the methodology of design by contract pay close attention to invariant quantities in computer programs, where the set of transformations involved is the execution of the steps of the computer program. ... In programming languages a data type defines a set of values and the allowable operations on those values[1]. For example, in the Java programming language, the int type represents the set of 32-bit integers ranging in value from -2,147,483,648 to 2,147,483,647, and...


Some languages allow an implementation of a class to specify 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. 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. In some languages, a destructor can return a value which can then be used to obtain a public representation (transfer encoding) of an object of a class and simultaneously destroy the copy of the object stored in current thread's memory. Constructors and destructors are also sometimes used to reserve and release resources associated with the object. In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in Java and other object-oriented languages) or dynamically constructed... In object-oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. ...


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.


Run-time representation of classes

As a datatype, a class is usually considered as a compile-time construct. A language may also support prototype or factory metaobjects that represent run-time information about classes, or even represent metadata that provides access to reflection facilities and ability to manipulate data structure formats at run-time. Many languages distinguish this kind of run-time type information about classes from a class on the basis that the information is not needed at run-time. Some dynamic languages do not make strict distinctions between run-time and compile-time constructs, and therefore may not distinguish between metaobjects and classes. For other uses, see Prototype (disambiguation). ... The factory method pattern is an object-oriented design pattern. ... In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. ... In computer science, reflection is the process by which a computer program of the appropriate type can be modified in the process of being executed, in a manner that depends on abstract features of its code and its runtime behavior. ... In programming, Runtime Type Information (RTTI, RunTime Type Identification) means keeping information about an objects datatype in memory at runtime. ...


For example: if humans is a metaobject representing the class Person, then instances of class Person can be created by using the facilities of the human metaobject. This article is about modern humans. ... In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. ... In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. ...


Information hiding and encapsulation

Main article: Information hiding

Many languages support the concept of information hiding and encapsulation, typically with access specifiers for class members. Access specifiers specify constraints on who can access which class members. Some access specifiers may also control how classes inherit such constraints. Their primary purpose is to separate the interface of a class with its implementation. In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. ...


A common set of access specifiers that many object-oriented languages support is:

  • Private restricts the access to the class itself. Only methods that are part of the same class can access private members.
  • Protected allows the class itself and all its subclasses to access the member.
  • Public means that all clients can access the member by its name.

Note that although many languages support these access specifiers, the semantics of them may subtly differ in each.


A common usage of access specifiers is to separate the internal data structures of a class from its interface; that is, the internal data structures are private. Public accessor methods can be used to inspect or alter such private 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. Java language redirects here. ... Objective-C, often referred to as ObjC or more seldomly as Objective C or Obj-C, is an object oriented programming language implemented as an extension to C. It is used primarily on Mac OS X and GNUstep, two environments based on the OpenStep standard, and is the primary language... Wikibooks has a book on the topic of Perl Programming Perl is a dynamic programming language created by Larry Wall and first released in 1987. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ...


Access specifiers do not necessarily control visibility, in that even private members may be visible to client code. In some languages, an inaccessible but visible member may be referred to at run-time (e.g. pointer to it can be returned from member functions), but all attempts to use it by referring to the name of the member from client code will be prevented by the type checker. Object-oriented design uses the access specifiers in conjunction with careful design of public method implementations to enforce class invariants. Access specifiers are intended to protect against accidental use of members by clients, but are not suitable for run-time protection of object's data.


In addition, some languages, such as C++, support a mechanism where a function explicitly declared as friend of the class may access the members designated as private or protected.


Associations between classes

In object-oriented design and in UML, an association between two classes is a type of a link between the corresponding objects. A (two-way) association between classes A and B describes a relationship between each object of class A and some objects of class B, and vice versa. Associations are often named with a verb, such as "subscribes-to". In Object_oriented programming, Association defines a relationship between classes of objects which allows one object instance to cause another to perform an action on its behalf. ... It has been suggested that this article or section be merged into Object-oriented analysis and design. ... In software engineering, Unified Modeling Language (UML) is a non-proprietary, third generation modeling and specification language. ...


An association role type describes the role type of an instance of a class when the instance participates in an association. An association role type is related to each end of the association. A role describes an instance of a class from the point of view of a situation in which the instance participates in the association. Role types are collections of role (instance)s grouped by their similar properties. For example, a "subscriber" role type describes the property common to instances of the class "Person" when they participate in a "subscribes-to" relationship with the class "Magazine". Also, a "Magazine" has the "subscribed magazine" role type when the subscribers subscribe-to it.


Association role multiplicity describes how many instances correspond to each instance of the other class(es) of the association. Common multiplicities are "0..1", "1..1", "1..*" and "0..*", where the "*" specifies any number of instances.


There are some special kinds of associations between classes.


Composition

Main article: Object composition

Composition between class A and class B describes a "part-of" relationship where instances of class B have shorter lifetime than the lifetime of the corresponding instances of the enclosing class. Class B is said to be a part of class A. This is often implemented in programming languages by allocating the data storage of instances of class A to contain a representation of instances of class B. This article does not cite any references or sources. ... In computer science, the object lifetime (or life cycle) of an object in object-oriented programming is the time between an objects creation (also known as instantiation or construction) till the object is no longer used, and is destructed or freed. ...


Aggregation is a variation of composition that describes that instances of a class are part of instances of the other class, but the constraint on lifetime of the instances is not required. The implementation of aggregation is often via a pointer or reference to the contained instance. In both cases, method implementations of the enclosing class can invoke methods of the part class. A common example of aggregation is a list class. When a list's lifetime is over, it does not necessarily mean the lifetimes of the objects within the list are also over. In computer science, a pointer is a programming language data type whose value refers directly to (or “points to”) another value stored elsewhere in the computer memory using its address. ...


Inheritance

Another type of class association 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 [station wagon] and [mini-van] might be two subclasses. If [Button] is a subclass of [Control], then all buttons are controls. Subclasses usually consist of several kinds of modifications (customizations) to their respective superclasses: addition of new instance variables, addition of new methods and overriding of existing methods to support the new instance variables. This article or section does not cite any references or sources. ... It has been suggested that CallSuper be merged into this article or section. ...


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. Subclassing is different from subtyping in that subtyping deals with common behaviour whereas subclassing is concerned with common structure. Reuse is using an item more than once. ... In computer science, a subtype states that if given type A is compatible with type B, then A is a subtype of B while not always vice versa. ...


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, as its designers felt that it would add unnecessary complexity. Java instead allows inheriting from multiple abstract classes (called interfaces in Java). C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ... Multiple inheritance refers to a feature of object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. ... Java language redirects here. ... In object-oriented programming, a class consists of encapsulated instance variables and subprograms, the methods mentioned below. ...


Sub- and superclasses are considered to exist within a hierarchy defined by the inheritance relationship. If multiple inheritance is allowed, this hierarchy is a directed acyclic graph (or DAG for short), otherwise it is a tree. The hierarchy has classes as nodes and inheritance relationships as links. The levels of this hierarchy are called layers or levels of abstraction. Classes in the same level are more likely to be associated than classes in different levels. In computer sciences object-oriented programming, the mapped relationships of sub- and superclasses is known as a hierarchy. ... A simple directed acyclic graph In computer science and mathematics, a directed acyclic graph, also called a dag or DAG, is a directed graph with no directed cycles; that is, for any vertex v, there is no nonempty directed path that starts and ends on v. ... A labeled tree with 6 vertices and 5 edges In graph theory, a tree is a graph in which any two vertices are connected by exactly one path. ... In object-oriented design, a layer is a group of classes that have the same set of link-time module dependencies to other modules. ... It has been suggested that this article or section be merged into Abstraction layer. ... In Object_oriented programming, Association defines a relationship between classes of objects which allows one object instance to cause another to perform an action on its behalf. ...


There are two slightly different points of view as to whether subclasses of the same class are required to be disjoint. Sometimes, subclasses of a particular class are considered to be completely disjoint. That is, every instance of a class has exactly one most-derived class, which is a subclass of every class that the instance has. This view does not allow dynamic change of object's class, as objects are assumed to be created with a fixed most-derived class. The basis for not allowing changes to object's class is that the class is a compile-time type, which does not usually change at runtime, and polymorphism is utilized for any dynamic change to the object's behavior, so this ability is not necessary. And design that does not need to perform changes to object's type will be more robust and easy-to-use from the point of view of the users of the class. In simple terms, polymorphism lets you treat derived class members just like their parent classs members. ...


From another point of view, subclasses are not required to be disjoint. Then there is no concept of a most-derived class, and all types in the inheritance hierarchy that are types of the instance are considered to be equally types of the instance. This view is based on a dynamic classification of objects, such that an object may change its class at runtime. Then object's class is considered to be its current structure, but changes to it are allowed. The basis for allowing changes to object's class is a perceived inconvenience caused by replacing an instance with an another instance of a different type, since this would require change of all references to the original instance to be changed to refer to the new instance. When changing the object's class, references to the existing instances do not need to be replaced with references to new instances when the class of the object changes. However, this ability is not readily available in all programming languages. This analysis depends on the proposition that dynamic changes to object structure are common. This may or may not be the case in practice.


Languages without inheritance

Although class-based languages are commonly assumed to support inheritance, inheritance is not an intrinsic aspect of the concept of classes. There are languages that support classes yet do not support inheritance. Examples are earlier versions of Visual Basic. These languages, sometimes called "object-based languages", do not provide the structural benefits of statically type-checked interfaces for objects. This is because in object-based languages, it is possible to use and extend data structures and attach methods to them at run-time. This precludes the compiler or interpreter from being able to check the type information specified in the source code as the type is built dynamically and not defined statically. Most of these languages allow for instance behaviour and complex operational polymorphism (see dynamic dispatch and polymorphism). This article is about the Visual Basic language shipping with Microsoft Visual Studio 6. ... In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime. ... In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects). ...


Categories of classes

There are many categories of classes. Note that these categories do not necessarily categorize classes into distinct partitions. For example, while it is impossible to have a class that is both abstract and concrete, a sealed class is implicitly a concrete class, and it may be possible to have an abstract partial class.


Concrete classes

A concrete class is a class that can be instantiated. This contrasts with abstract classes as described below.


Abstract classes

Main article: Abstract type

An abstract class, or abstract base class (ABC), is a class that cannot be instantiated. Such a class is only meaningful if the language supports inheritance. An abstract class is designed only as a parent class from which child classes may be derived. 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 subclasses which add different variations of the missing pieces. In software engineering, an abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are also not members of some declared subtype. ... In computer science, abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time. ...


Abstract classes are superclasses which contain abstract methods and are defined such that concrete subclasses are to extend them by implementing the methods. 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 become concrete, i.e. a class that can be instantiated, it must implement particular methods for all the abstract methods of its parent classes. Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods or static methods) or with an object (called instance methods). ...


When specifying an abstract class, the programmer is referring to a class which has elements that are meant to be implemented by inheritance. The abstraction of the class methods to be implemented by the subclasses is meant to simplify software development. This also enables the programmer to focus on planning and design. “Software development” redirects here. ...


Most object oriented programming languages allow the programmer to specify which classes are considered abstract and will not allow these to be instantiated. For example, in Java, the keyword abstract is used. In C++, an abstract class is a class having at least one abstract method (a pure virtual function in C++ parlance). Java language redirects here. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ...


Some languages, notably Java, additionally support a variant of abstract classes called an interface. Such a class can only contain abstract publicly-accessible methods. In this way, they are closely related - but not equivalent - to the abstract concept of interfaces described in this article. An interface in the Java programming language is an abstract type which is used to specify an interface (in the generic sense of the term) that classes must implement. ...


Sealed classes

Some languages also support sealed classes. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. They add another level of strictness during compile-time, improve memory usage, and trigger certain optimizations that improve run-time efficiency.


Local and inner classes

In some languages, classes can be declared in scopes other than the global scope. There are various types of such classes. In computer programming, scope is an enclosing context where values and expressions are associated. ...


One common type is an inner class or nested class, which is a class defined within another class. Since it involves two classes, this can also be treated as another type of class association. The methods of an inner class can access static methods of the enclosing class(es). An inner class is typically not associated with instances of the enclosing class, i.e. an inner class is not instantiated along with its enclosing class. Depending on language, it may or may not be possible to refer to the class from outside the enclosing class. A related concept is inner types (a.k.a. inner datatype, nested type), which is a generalization of the concept of inner classes. C++ is an example of a language that supports both inner classes and inner types (via typedef declarations). C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ...


Another type is a local class, which is a class defined within a procedure or function. This limits references to the class name to within the scope where the class is declared. Depending on the semantic rules of the language, there may be additional restrictions on local classes compared non-local ones. One common restriction is to disallow local class methods to access local variables of the enclosing function. For example, in C++, a local class may refer to static variables declared within its enclosing function, but may not access the function's automatic variables.


Metaclasses

Main article: Metaclass

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. In object-oriented programming, a metaclass is a class whose instances are classes. ... In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. ... Look up Framework in Wiktionary, the free dictionary. ...


In some languages such as Smalltalk and Ruby, a class is also an object; thus each class is an instance of the unique metaclass, which is built in the language. For example, in Objective-C, each object and class is an instance of NSObject. The Common Lisp Object System (CLOS) provides metaobject protocols (MOPs) to implement those classes and metaclasses. For other uses, see Small talk. ... Ruby is a reflective, dynamic, object-oriented programming language. ... Objective-C, often referred to as ObjC or more seldomly as Objective C or Obj-C, is an object oriented programming language implemented as an extension to C. It is used primarily on Mac OS X and GNUstep, two environments based on the OpenStep standard, and is the primary language... Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard X3. ... The Common Lisp Object System (CLOS) is the facility for object-oriented programming which is part of Common Lisp (CL). ... In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. ...


Partial classes

Main article: Partial class

Partial classes are classes that can be split over multiple definitions (typically over multiple files), making it easier to deal with large quantities of code. At compile time the partial classes are grouped together, thus logically make no difference to the output. An example of the use of partial classes may be the separation of user interface logic and processing logic. A primary benefit of partial classes is allowing different programmers to work on different parts of the same class at the same time. They also make automatically generated code easier to interpret, as it is separated from other code into a partial class. A partial class, or partial type, is a feature of some object oriented computer programming languages in which the declaration of a class may be split across multiple source-code files, or multiple places within a single file. ... A diagram of the operation of a typical multi-language, multi-target compiler. ...


Partial classes have been around in SmallTalk under the name of Class Extensions for considerable time. With the arrival of the .NET framework 2, Microsoft introduced partial classes, supported in both C# 2.0 and Visual Basic 2005. For other uses, see Small talk. ... The Microsoft . ... Microsoft Corporation, (NASDAQ: MSFT, HKSE: 4338) is a multinational computer technology corporation with global annual revenue of US$44. ... The title given to this article is incorrect due to technical limitations. ... Visual Basic . ...


Non-class-based object-oriented programming

To the surprise of some familiar with the use of classes, classes are not the only way to approach object-oriented programming. Another common approach is prototype-based programming. Languages that support non-class-based programming are usually designed with the motive to address the problem of tight-coupling between implementations and interfaces due to the use of classes. For example, the Self language, a prototype-based language, was designed to show that the role of a class can be substituted by using an extant object which serves as a prototype to a new object, and the resulting language is as expressive as Smalltalk with more generality in creating objects. Prototype-based programming is a style of object-oriented programming in which classes are not present, and behaviour reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. ... This article or section does not cite its references or sources. ... For other uses, see Small talk. ...


Examples

C++

Example 1

 #include <iostream> #include <string> using namespace std; class Hello { string what; public: Hello(const char* s) : what(s) { } void say() { cout << "Hello " << what << "!" << endl; } }; int main( int argc, char** argv ) { Hello hello_world("world"); hello_world.say(); return 0; } 

This example shows how to define a C++ class named "Hello". It has a private string attribute named "what", and a public method named "say". C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ...


Example 2

 class MyAbstractClass { public: virtual void MyVirtualMethod() = 0; }; class MyConcreteClass : public MyAbstractClass { public: void MyVirtualMethod() { //do something } }; 

An object of class MyAbstractClass cannot be created because the function MyVirtualMethod has not been defined (the =0 is C++ syntax for a pure virtual function, a function that must be part of any derived concrete class but is not defined in the abstract base class. The MyConcreteClass class is a concrete class because its functions (in this case, only one function) have been declared and implemented.


Example 3

 #include <string> #include <iostream> using namespace std; class InetMessage { string m_subject, m_to, m_from; public: InetMessage (const string& subject, const string& to, const string& from) : m_subject(subject), m_to(to), m_from(from) { } string subject () const { return m_subject; } string to () const { return m_to; } string from () const { return m_from; } virtual void printTo(ostream &out) { out << "Subject: " << m_subject << endl; out << "From: " << m_from << endl; out << "To: " << m_to << endl; } }; 

Java

For more information on the programming language, see Java (programming language). For more information on Java .class files, see Class (file format).

Java language redirects here. ... The Java source files (.java files) get compiled into . ...

Example 1

 public class Example1 { // This is a Java class, it automatically extends the class Object public static void main (String args[]) { System.out.println("Hello world!"); } } 

This example shows the simplest Java program possible. Java language redirects here. ...


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

This example shows a class that has a defined constructor, one member data, an accessor method (getData) and a Modifier method (setData) for that member data. It extends the previous 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.


REALbasic

Example 1

 Class Hello Private Dim what as String Sub Constructor( s as String ) what = s End Sub Sub Say() MsgBox "Hello " + what + EndOfLine End Sub End Class Sub App.Open() dim h as new Hello( "Foobar" ) h.Say End Sub 

This example is a port of the C++ example above. It demonstrates how to make a class named Hello with a private property named what. It also demonstrates the proper use of a constructor, and has a public method named Say. It also demonstrates how to instantiate the class and call the Say method.


PHP

Example 1

 <?php class A { public function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")n"; } else { echo "$this is not defined.n"; } } } ?> 

Example 2

 <?php class date { private $date; public function __construct() { $this->date = date('c'); } private function getDate() { return $this->date; } public function printDate() { echo 'Todays Date is ' . $this->getDate() . 'n'; } } ?> 

C#

Example 1

 using System; public class Program { public static void Main(string[] args) { System.Console.WriteLine("Hello world!"); } } 

This example demonstrates a traditional "Hello world!" example in Microsoft's C# language. The Program class contains a single static method, Main(), which calls System.Console.WriteLine to print text onto the console.


Example 2

 using System; public class Hello { private string what; public Hello(string s) { what = s; } public void Say() { Console.WriteLine("Hello " + what + "!"); } } public class Program { public static void Main(string[] args) { Hello helloWorld = new Hello("world"); helloWorld.Say(); // prints "Hello world!" onto the console } } 

This is another port of the above C++ example. A class called Hello is created with a constructor that takes a string parameter. When the Say() method is called, the instance of Hello will print "Hello {what}!" onto the console. Notice that the Main() method (the entry point) is actually contained in a class itself.


Actionscript

Example 1

 class Cart { private var cart:Array; function Cart() { this.cart = new Array(); } public function addItem(id:Number, name:String):Void { this.cart.push([id, name]); } public function removeItemById(id:Number):Void { var ln:Number = this.cart.length; for (var i:Number = 0; i<ln; i++) { var curr:Array = this.cart[i]; if (curr[0] == id) this.cart.splice(i, 1); } } public function removeItemByName(name:String):Void { var ln:Number = this.cart.length; for (var i:Number = 0; i<ln; i++) { var curr:Array = this.cart[i]; if (curr[1] == name) this.cart.splice(i, 1); } } } 

See also

In computer sciences object-oriented programming, the mapped relationships of sub- and superclasses is known as a hierarchy. ... Hierarchy of UML 2. ... Class-based programming, or more commonly class-orientation, refers to the style of object-oriented programming in which inheritance is achieved by defining classes of objects, as opposed to the objects themselves (compare Prototype-based programming). ... In computer science, Object-based has two different, non compatible, senses: A) A limited version of object-oriented programming where one or more of the following restrictions applies: there is no implicit inheritance there is no polymorphism only a very reduced subset of the available values are objects (typically the...

Literature


  Results from FactBites:
 
class: Information from Answers.com (4259 words)
A class describes a collection of encapsulated instance variables and methods (functions), possibly with implementation of those types together with a constructor function that can be used to create objects of 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.
Composition between class A and class B describes a "part-of" relationship where instances of class B have shorter lifetime than the lifetime of the corresponding instances of the enclosing class.
DEAS - Undergraduate Study - Computer Science - Frequently Asked Questions (761 words)
The computer science department has 19 faculty members and is steadily growing.
A typical computer science concentration entails 12 courses.
Classes can be graded on the basis of exams, written assignments, programming assignments and final projects.
  More results at FactBites »

 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your location
Your comments
Please enter the 5-letter protection code


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.