THE NATURE OF A CLASS

WHAT IS AND WHAT ISN’T A CLASS

The concept of a class and an objects are tightly interowens,for we cannot talk about an object without regard for its class .However there are important difference between these two terms. Where as an objects is a concrete entity that exists in time and space a class represents only an abstraction,”the essence of an objects”, as it were. Thus we may speak of the class mammals. To identify a particular mammal in this class, we must speak of “this mammal”or “that mammal”.

Definition: A class is a set of objects that share a common structure and a common behavior.

A single object is simply an instance of a class .An object is not a class, although a class may be an object. Object that share no common structure and behavior cannot be grouped in a class because, by definition, they are unrelated except by general nature as object.

It is important to note that the class—as defined by most programming languages —is a necessary but insufficient vehicle for decomposition .Sometimes abstractions are so complex that they cannot be conveniently expressed in terms of a single declaration. For example, at a sufficiently high level of abstraction, a UGI frame work, a database ,and an entire inventory system are all conceptually individual objects , none of which can be expressed as a single class. Instead ,it is far better for us to capture these abstractions as a cluster of classes whose instances collaborate to provide the desired structure and behavior.

Examples of class and objects:

As all human beings have common features ,we can have a class of human beings .Let’s say it is X.An instance of X class can be an existing person whose has the features of class X.Let’s call it Y.Therefore X is the class and Y is the object.

INTERFACE AND IMPLEMENTATION

Programming is largely a matter of “contracting”: the various functions of a larger problem are decomposed into smaller problems by subcontracting them to different elements of the design. No where is this idea more evident then in the design of the classes.

Where as an individual object is an concrete entity that performs some role in the overall systems, the class captures the structure and behavior common to all related objects. Thus , a class serves as a sort of binding contract between an abstraction and all of its clients. By capturing these decisions in the interface of a class , a strongly typed programming language can detect violations of these contract during compilation.

This view of programming as contracting leads us to distinguish between the outside view and the inside view of a class.The interface of a class provides its outside view and therefore emphasis the abstraction while hiding its structure and the secrete of its behavior. This interface primarily consists of the declarations of all the operations applicable to instance of this class, but it may also include the declaration of other classes ,constants, variables, and exceptions as needed to complete the abstractions. By contrast ,the implementation of a class is its inside view, which encompasses the secrets of its behavior .The implementation of a class primarily consists of the implementation of all the operations defined in the interface of the class.

We can further divided the interface of a class into three parts:

Public A declaration that is accessible to all clients

Protected A declaration that is accessible only to the class itself,

Private A declaration that is accessible only to the class itself and its friends.

Different programming languages provide different mixture of public, protected and private parts, which developers can choose among to establish specific access rights for each part of a classes interface and thereby exercise over what clients can see and what they can’t see.

In particular ,C++ allows a developer to make explicit distinctions among all three of these parts. The C++ friendship mechanism permits a class to distinguish certain privileged classes that are given the rights to see the classes protected and private parts.Friendship breaks a classes encapsulation, and so in life, must be chosen carefully.


« Previous PageNext Page »