3.4 Classes


3.4 Classes

In the real-world problem, a class describes a collection of real-world entities or objects with similar characteristics. The abstract descriptions of the collections of objects are called classes (or class models).

Figure 3.6 illustrates the identifying of two collections of real-world objects, the modeling of the classes, and the software implementation of these classes.

click to expand
Figure 3.6: Collections of real-world objects.

Note

Each class model describes a collection of similar real-world objects. Collections of such objects are used to distinguish one type of object from another. The model of a class is represented graphically in UML as a class diagram.

Collections of objects are modeled as classes. An object belongs to a collection or class, and any object of the class is an instance of the class. Every class defines:

  • Attributes (data declarations)

  • One or more operations (also known as functions and methods)

A complete object-oriented model of an application consists of a description of all the classes and their relationships, the objects and their interactions, and a complete documentation of these.

A class defines the attributes and behavior for all the objects of the class. Figure 3.7 shows the diagram for class Person. Figure 3.8 shows the UML diagram for class Ball.


Figure 3.7: Class Person.

click to expand
Figure 3.8: Class Ball.

3.4.1 Encapsulation

The encapsulation principle suggests that an object be described as the integration of attributes and behavior in a single unit. There is an imaginary wall surrounding the object to protect it from another object. This is considered an encapsulation protection mechanism. To protect the features of an object, an access mode is specified for every feature.

When access to some of the attributes and some operations is not allowed, the access mode of the feature is specified to be private; otherwise, the access mode is public. If an operation of an object is public, it is accessible from other objects. Figure 3.9 illustrates the notion of an object as an encapsulation unit.

click to expand
Figure 3.9: An encapsulation unit.

3.4.2 Information Hiding

An object that provides a set of services to other objects is known as a provider object, and all other objects that request these services by sending messages are known as client objects. An object can be a service provider for some services, and it can also be a client for services that it requests from other (provider) objects.

In describing the objects of a class, information hiding is the principle of only showing what services the object provides and hiding all implementation details. In this manner, an object presents two views:

  1. The external view of the object can be shown to all client objects. This view consists of the list of services (or operations) that other objects can invoke. The list of services can be used as a service contract between the provider object and the client objects.

  2. The internal view presents the implementation details of the data and the operations of the object. This information is hidden from other objects.

These two views of an object are described at two different levels of abstraction. The external view is at a higher level of abstraction.

The external view implies that information about an object is limited to that only necessary for the object's features to be invoked by another object. The rest of the knowledge about the object is not revealed. As mentioned earlier, this principle is called information hiding or data hiding.

Note

In the class definition, the external view of the objects should be kept separate from the internal view. The internal view of properties and operations of an object are hidden from other objects. The object presents its external view to other objects and shows what features (operations and attributes) are accessible.

The public features of an object are accessible to other objects, but the implementation details of these features are kept hidden. In general, only the headers, that is, the specification, of the operations are known. Similarly, only the public attributes are the ones accessible from other objects.

The models of objects are represented using the Unified Modeling Language (UML), the standard graphical notation introduced previously in this chapter. A basic part of this notation is used in this book to describe object-oriented models. Because every object belongs to a class, the complete description of the objects is included in the corresponding class definitions.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net