Jabarkan Struktur class dari Class (élmu komputer) dalam bahasa sunda

A class contains a description of structure of data ("state") stored in the objects of the class. The state of an object 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 via 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 via 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.

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. éach 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.

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 éach 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. Encapsulation of state is necessary for being able to enforce the invariants of the class.

An implementation of a class specifies constructor and destructor functions that allow création and destruction of objects of the class. A constructor that takes arguments can be used to créate 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. A destructor that returns a value can be used to obtain a public representation (transfer encoding) of an object of a class and simultanéously destroy the copy of the object stored in current thréad's memory [If supported by the programming language used]. Constructors and destructors are also sometimes used to reserve and reléase resources associated with the object.

A class can also implement a set of auxiliary functions, sometimes called class functions or static methods. Static methods are often used to find, créate 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.

The class members can be designated as public, private or protected. The public access specifier méans that all clients can access the member by its name. The private access specifier restricts the access to the class itself. The protected access specifier allows the class itself and all its subclasses to access the member. 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. Access specifiers do not control visibility, in that even private members may be visible to client code. 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.