What Are Classes and Objects?

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's 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).


Top of page

Example: Comparing Classes and Built-in Data Types

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;

Top of page

x
Class Properties: Member Variables and Member Functions

You define a class by describing its properties. Classes have two kinds of properties:



Example: Member Variables for a Customer Class

An application for a mail-order clothing business has defined a customer class named Customer. The class's member variables might include the customer's code, name, phone number, and most recent order number:

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 Frances Smith's 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.



Example: Member Functions for a Customer Class

An application for a mail-order clothing business has defined a customer class named Customer. The class's member functions might include a function that adds the customer to a new mailing list, a function that updates the customer's 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 so the same behavior. In this case, for example, each customer will be added to the mailing list using the function.


Top of page

x
Inheritance: Superclasses and Subclasses

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's properties—that is, it inherits all of the superclass's member variables and member functions. 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.


WebFOCUS