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 visa versa. For example, a specific individual bike is an object. However, someone had to have created 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, Objective C, or C++), but it is often helpful to use code examples to explain some concepts, so Java code fragments are used throughout the book to help explain some concepts when appropriate. However, when appropriate, the end of each chapter contains the chapter’s example code in C#. Much of the code presented in the book is available electronically on the publisher’s website. For many chapters, the code examples are provided electronically in Java, C# .Net, VB .NET, and Objective C.
The following sections describe some of the fundamental concepts of classes and how they interact.
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.
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;
In this example, the names themselves make it obvious that myClass
is the class and myObject
is the object.
Remember that each object has its own attributes (data) and behaviors (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 (attributes, 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;
}
}
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
.
As you learned earlier in the chapter, methods implement the required behavior of a class. Every object instantiated from this class has the methods as defined by the class. Methods may implement behaviors that are called from other objects (messages) or provide the fundamental, 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 technique in OO systems. In all cases, access to attributes within an object should be controlled by the object itself—no other object should directly change an attribute of another.
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, because we are really interested in the concepts. We address the code in detail as we progress through the book.
3.142.49.90