November 22, 2014
Hot Topics:

Moving from Procedural to Object-Oriented Development

  • February 25, 2004
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

We will get into the relationships between classes and objects later, but for now you can think of a class as a template from which objects are made. When an object is created, we say that the objects are instantiated. Thus, if we create three employees, we are actually creating three totally distinct instances of an Employee class. Each object contains its own copy of the attributes and methods. For example, consider Figure 6. An employee object called John (John is its identity) has its own copy of all the attributes and methods defined in the Employee class. An employee object called Mary has its own copy of attributes and methods. They both have a separate copy of the DateOfBirth attribute and the getDateOfBirth() method.

Figure 6 - Program spaces.

Physical Copies

Be aware that there is not necessarily a physical copy of each method for each object. Rather, each object points to the same physical code. However, this is an implementation issue left up to the compiler/operating platform. From a conceptual level, you can think of objects as being wholly independent and having their own attributes and methods.

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, 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 series 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 series 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 7. A class is used to create an object.

Figure 7 - Class template.

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 also can 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. You'll learn about this later in the series.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel