What Exactly Is a Class?

 <  Day Day Up  >  

In short, a class is a blueprint for an object. When you instantiate an object, you use a class as the basis for how the object is built. In fact, trying to explain classes and objects is really a chicken-and-egg dilemma. It is difficult to describe a class without using the term object , and to describe an object without using the term class . For example, a specific bike is an object. However, someone had to have the blueprints (that is, the class) to build the bike. In OO software, unlike the chicken-and-egg dilemma, we do know what comes first ”the class. An object cannot be instantiated without a class. Thus, many of the concepts in this section are similar to those presented earlier in the chapter, especially when we talk about attributes and methods .

To explain classes and methods, it's helpful to use an example from the relational database world. In a database table, the definition of the table itself (fields, description, and data types used) would be a class (metadata), and the objects would be the rows of the table (data).

This book focuses on the concepts of OO software, and not on a specific implementation (such as Java, C#, Visual Basic .NET or C++), but it is often helpful to use code examples to explain some concepts, so Java code fragments are used in this chapter to help explain some concepts when appropriate. The following sections describe some of the fundamental concepts of classes and how they interact.

Classes Are Object Templates

Classes can be thought of as the templates, or cookie cutters, for objects as seen in Figure 1.10. A class is used to create an object.

Figure 1.10. Class template.

graphics/01fig10.gif

A class can be thought of as a sort of higher-level data type. For example, just as you create an integer or a float:

 
 int x; float y; 

You can also create an object by using a predefined class:

 
 myClass myObject; 

Because of the names used in this example, it is obvious that myClass is the class and myObject is the object.

Remember that each object has its own attributes (analogous to fields) and behaviors (analogous to functions or routines). A class defines the attributes and behaviors that all objects created with this class will possess. Classes are pieces of code. Objects instantiated from classes can be distributed individually or as part of a library. Because objects are created from classes, it follows that classes must define the basic building blocks of objects (data, behavior, and messages). In short, you must design a class before you can create an object.

For example, here is a definition of a Person class:

 
 public class Person{     //Attributes     private String name;     private String address;     //Methods     public String getName(){         return name;     }     public void setName(String n){         name = n;     }     public String getAddress(){         return address;     }     public void setAddress(String adr){         address = adr;     } } 

Attributes

As you already saw, the data of a class is represented by attributes. Each class must define the attributes that will store the state of each object instantiated from that class. In the Person class example in the previous section, the Person class defines attributes for name and address .

Access Designations

When a data type or method is defined as public , other objects can directly access it. When a data type or method is defined as private , only that specific object can access it. Another access modifier, protected , allows access by related objects, which you'll learn about in Chapter 3.


Methods

As you learned earlier in the chapter, methods implement the required behavior of a class. Every object instantiated from this class has these methods. Methods may implement behaviors that are called from other objects (for example, messages) or provide the internal behavior of the class. Internal behaviors are private methods that are not accessible by other objects. In the Person class, the behaviors are getName() , setName() , getAddress() , and setAddress() . These methods allow other objects to inspect and change the values of the object's attributes. This is common design in OO systems. In all cases, access to attributes within an object should be controlled by the object ”no other object should directly change an attribute of another.

Messages

Messages are the communication mechanism between objects. For example, when Object A invokes a method of Object B, Object A is sending a message to Object B. Object B's response is defined by its return value. Only the public methods, not the private methods, of an object can be invoked by another object. The following code illustrates this concept:

 
 public class Payroll{     String name;     Person p = new Person();     String = p.setName("Joe");     ... code     String = p.getName(); } 

In this example ( assuming that a Payroll object is instantiated), the Payroll object is sending a message to a Person object, with the purpose of retrieving the name via the getName method. Again, don't worry too much about the actual code, as we are really interested in the concepts. We will address the code in detail as we progress through the book.

 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

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