In this section: |
Most applications need many objects of the same type. For example, if your business has 500 customers, you need one object to represent each customer. No one would want to design a customer object 500 times. Clearly, you need a template that defines all customer objects, so that you can design the template once, and use it often. For example, you would use the template each time you create a new customer object to represent a new customer.
An object template is called its class. Each object is an instance of a class. In other words, the class defines the type of object. In fact, when you create a class, the class becomes a new data type. Just as you can use a built-in data type, such as integer or alphanumeric, to define a simple variable, you can use a class data type to define an object.
Unlike a Master File, which is also a kind of template, a class defines both variables and functions. Just as a built-in data type defines the operations that you can perform on data of that type (for example, you can perform addition, subtraction, division, and multiplication on integers), a class defines the functions that you can perform on objects of that class (for example, you can invoke functions to update an address and to place an order for a customer object).
Just as you can use the alphanumeric built-in data type to define a customer ID code as an A8 variable
DECLARE CustID/A8;
you can use the RetailCustomer class to define a customer as an object:
DECLARE CustSmit8942/RetailCustomer;
You define a class by describing its properties. Classes have two kinds of properties:
Class member variables determine what the class is (as opposed to what it does). Each object of that class can have different values for its member variables.
The member function of a class defines its behavior. They determine what you can do to objects of that class, and in what ways you can manipulate the data.
An application for a mail-order clothing business has defined a customer class named Customer. The member variables of the class might include the customer code, name, phone number, and most recent order number of the customer:
DESCRIBE Customer = (IDcode/A6, LastName/A15, FirstName/A10, Phone/I10, LastOrder/A15); . . . ENDDESCRIBE
After declaring a new customer object for the customer, Frances Smith
DECLARE CustFrSmith/Customer;
you can assign a value to the IDcode member variable:
DECLARE CustFrSmith.IDcode = GetNewCustCode();
Each object can have different values for its member variables. For example, in this case, each customer will have a different ID code.
An application for a mail-order clothing business has defined a customer class named Customer. The class member functions might include a function that adds the customer to a new mailing list, a function that updates the customer contact information, and a function that places an order for the customer:
DESCRIBE Customer = (IDcode/A6, Phone/I10, . . . LastOrder/A15); CASE AddToList TAKES Name/A25, Address/A50, IDcode/A6; . . . ENDCASE CASE UpdateContact ...
CASE PlaceOrder ...
ENDDESCRIBE
After declaring a new customer object for the customer Frances Smith
DECLARE CustFrSmith/Customer;
you could add Frances Smith to the mailing list, using the AddToList member function:
CustFrSmith.AddToList();
Each object has the same member functions, and therefore, the same behavior. In this case, for example, each customer will be added to the mailing list using the function.
If you want to create a new class that is a special case of an existing class, you could derive it from the existing class. For example, in a human resources application, a class called Manager could be considered a special case of a more general class called Employee. All managers are employees, and posses all employee attributes, plus some additional attributes unique to managers. The Manager class is derived from the Employee class, so Manager is a subclass of Employee, and Employee is the superclass of Manager.
A subclass inherits all of its superclass properties. For example, it inherits all of the member variables and member functions of the superclass. When you define a subclass, you can choose to override some of the inherited member functions, meaning that you can recode them to suit the ways in which the subclass differs from the superclass. You can also add new member functions and member variables that are unique to the subclass.