Session E-EOO

Introduction to Object Orientation

Jim Booth
Microsoft MVP 1993, 1994, 1995


What is Object Orientation?

Object Orientation is an approach to the analysis and design of software systems which involves the identification of self contained components which can be linked together to produce complete applications. These components achieve the encapsulation of data and function, the ability to inherit things from other components, and can communicate with each other by sending messages back and forth.

Object Oriented systems are created through the definition of Classes which are then used to create Objects. Edward Yourdan defines a class as:

Grady Booch defines an Object as:

The difference between a Class and an Object is found in the fact that Classes are the definition from which Objects are created. Objects exist and can communicate with other objects, but Classes only exist as blueprints from which Objects are instantiated.

There are certain qualities that are descriptive of an Object Oriented design. many of these qualities are also descriptive of other earlier design methods such as Modular or Structured, but none of the earlier design methodologies encompass all of these qualities.

This is the ability to present a complex problem in a simplified model. All programming methodologies include the ability to abstract the overall design in a simplified model, but OO takes this to the extreme.

The ability to completely hide the details of the implementation of a object’s behavior within the object by providing a public interface to the object and precluding any access to the objects methods and properties other than through that public interface.

The ability of an object to take all or part of its definition from another object.

The ability of an operation to apply to several objects. Polymorphism comes in two flavors, Inherent and Ad Hoc. Inherent polymorphism describes the ability of a single operation to be available to every subclass under a class hierarchical tree. Ad Hoc polymorphism describes the ability for differing operations to have the same name in different classes.

The concept describing the fact that as we walk down the tree of class definitions we find that the classes become more and more specialized in their operations. For example we might see a class hierarchy for Command Buttons like this;

1 Command Button

At each level of this tree the class is more specific than at the level above it.

The idea that all activity in an application is comprised of objects sending and receiving message to and from other objects.

The idea is of component reuse is not unique to Object Orientation. However, due to the other qualities of OO, reuse is considerably more achievable.


OO and VFP

Allows creation of classes in program code. The DEFINE CLASS command is used to begin a class definition, the definition will be ended with the ENDDEF command. Between these two commands you can define properties (variables) and methods (procedures) for the class. The syntax of the DEFINE CLASS command is as follows;

 

Following this command line we can add properties and methods to the class. The example below shows a simple class definition.

 

Creates an object from a class definition. Using the class MyClass from above we could create an object with this code;

The following code example defines the classes of Person and Employee. Person is a custom class and employee is a subclass of person.

The following example uses the class definitions from above to create two objects from the person class and then get them to talk to each other.

In the above examples you should notice that the classes are never referred except when creating an object. It is the objects that are addressed in the program. The methods and properties that are accessed belong to the objects that have been instantiated based on the class definitions.

Also note that the program never tries to change one of the properties of the objects, instead methods within the objects are called to make the changes. Even during the communication between the objects, one never directly changes the properties of the other, this is always done by calling a method of the object whose property we want to have changed and letting that object change its own property. This protects the encapsulation of the objects.

The abstraction is evidenced in fact that the class of person can be instantiated to produce many different people with differing property values. We can see that when we look at oJim and oJohn and notice that they have differing hair color and eye color.

Inheritance is seen by looking at the Employee class definition. The only method defined in the employee class is the one that needs something extra to deal with salary, and that method actually calls the person class’ method to finish the work. One property is defined for the employee, but an object instantiated from employee will have that one property (salary) and all of the other properties of the person class.

If you look at the Hello method of the person and employee classes you can see an example of polymorphism. The same method name, Hello, is used slightly differently in each class.

The employee as a subclass of person is an example of specialization. Employees are specialized types of persons.

The second code example shows how object communications are carried out by sending messages from one object to another and returning values back.