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.
What is a Class, Object?
Object Oriented systems are created through the definition of Classes which are then used to create Objects. Edward Yourdan defines a class as:
"A collection of one or more Objects with a uniform set of Attributes and Services, including a description of how to create new Objects in the Class."
Grady Booch defines an Object as:
"An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class; the terms instance and object are interchangeable"
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.
Qualities of Object Orientation
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.
Abstraction
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.
Encapsulation
The ability to completely hide the details of the implementation of a objects 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.
Inheritance
The ability of an object to take all or part of its definition from another object.
Polymorphism
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.
Specialization
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
1.1 Gray Command Button
1.1.1 Gray Exit Command Button
1.1.1.1 Gray Application Exit Command Button
At each level of this tree the class is more specific than at the level above it.
Object Communications
The idea that all activity in an application is comprised of objects sending and receiving message to and from other objects.
Reuse
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
Creating Classes in Code
DEFINE CLASS
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;
DEFINE CLASS <Class name> AS <Parent class name>
Following this command line we can add properties and methods to the class. The example below shows a simple class definition.
DEFINE CLASS MyClass AS Custom
PROTECTED cName
cName = "MyClass"
PROCEDURE SetName
LPARAMETERS cNewName
THIS.cName = cNewName
ENDPROC
ENDDEF
Creating Objects in Code
CreateObj()
Creates an object from a class definition. Using the class MyClass from above we could create an object with this code;
oMyObject = CREATEOBJ( "MyClass" )
Visual Classes (VCX)
CREATE CLASSLIB buttons.vcx
CREATE CLASS cmdButton OF Buttons.vcx AS commandbutton
CREATE CLASS cmdExit OF Buttons.vcx AS cmdbutton FROM Buttons.vcx
Visual Classes (VCX)
SAVE AS CLASS
Putting it together (Person and Employee)
Defining the class
The following code example defines the classes of Person and Employee. Person is a custom class and employee is a subclass of person.
DEFINE CLASS Person AS Custom
PROTECTED cHairColor,cEyeColor,cHeight,cAnswer,cRequest,cName
cHairColor = "Auburn"
cEyeColor = "Blue"
cHeight = "6 foot 2"
cAnswer = ""
cName = "John"
PROCEDURE Hello
lPARAMETERS pcMessage,pnTalk, pcMyName
IF EMPTY(pcMessage)
pcMessage = " "
ENDIF
IF EMPTY(pnTalk)
pnTalk = 0
ENDIF
DO CASE
CASE "HOW ARE YOU" $ UPPER(pcMessage)
cAnswer = "I am fine thank you."
CASE "HOW TALL" $ UPPER(pcMessage)
cAnswer = "I am " + THIS.cHeight
CASE "COLOR" $ UPPER(pcMessage) AND ;
"EYES" $ UPPER(pcMessage)
cAnswer = "My eyes are " + THIS.cEyeColor
CASE "COLOR" $ UPPER(pcMessage) AND ;
"HAIR" $ UPPER(pcMessage)
cAnswer = "My hair is " + THIS.cHairColor
OTHERWISE
cAnswer = "I don't understand."
ENDCASE
DO CASE
CASE pnTalk = 0
WAIT WINDOW cAnswer
CASE pnTalk = 2
WAIT WINDOW SUBSTR(pcMyName,2) + ": " + cAnswer
ENDCASE
RETURN cAnswer
ENDPROC
PROCEDURE TalkTo
lPARAMETERS pcWho, pcMessage
IF TYPE( pcWho ) = "U"
WAIT WINDOW pcWho + " is not available to talk to me."
RETURN
ENDIF
cRequest = pcWho + '.Hello("' + pcMessage + '",1, pcWho)'
cRequest = &cRequest
WAIT WINDOW SUBSTR(pcWho,2) + ' says "' + cRequest + '"'
ENDPROC
PROCEDURE Converse
Lparameters pcWho,pcMyName
IF TYPE( pcWho ) = "U"
WAIT WINDOW pcWho + " is not available to talk to me."
RETURN
ENDIF
PRIVATE lnCnt
FOR lnCnt = 1 TO 6
DO CASE
CASE lnCnt = 1
cMsg = "Hello, " + SUBSTR(pcWho, 2)
CASE lnCnt = 2
cMsg = "How are you today?"
CASE lnCnt = 3
cMsg = "How tall are you, " + SUBSTR(pcWho, 2) + "?"
CASE lnCnt = 4
cMsg = "What color eyes do you have?"
CASE lnCnt = 5
cMsg = "And your hair is what color?"
OTHERWISE
WAIT WINDOW SUBSTR(pcMyName,2) + ;
": Goodbye, " + SUBSTR(pcWho, 2)
RETURN
ENDCASE
WAIT WINDOW SUBSTR(pcMyName,2) + ": " + cMsg
cRequest = pcWho + '.Hello(cMsg ,2, pcWho)'
cRequest = &cRequest
ENDFOR
ENDPROC
PROCEDURE Contacts
lPARAMETERS pcColor
THIS.cEyeColor = pcColor
ENDPROC
PROCEDURE DyeHair
lPARAMETERS pcColor
THIS.cHairColor = pcColor
ENDPROC
PROCEDURE Measure
lPARAMETERS pcHeight
THIS.cHeight = pcHeight
ENDPROC
PROCEDURE YourName
lPARAMETERS pcName
THIS.cName = pcName
ENDPROC
PROCEDURE GetSalary
RETURN 0
ENDPROC
ENDDEFINE
DEFINE CLASS Employee AS PERSON
PROTECTED nSalary
nSalary = 25000
PROCEDURE Raise
lPARAMETERS pnSalary
THIS.nSalary = pnSalary
ENDPROC
PROCEDURE GetSalary
RETURN THIS.nSalary
ENDPROC
PROCEDURE Hello
lPARAMETERS pcMessage,pnTalk, pcMyName
IF EMPTY(pcMessage)
pcMessage = " "
ENDIF
IF EMPTY(pnTalk)
pnTalk = 0
ENDIF
IF "SALARY" $ UPPER( pcMessage )
cAnswer = "My salary is " + ;
TRANSFORM(THIS.nSalary,"$999,999")
DO CASE
CASE pnTalk = 0
WAIT WINDOW cAnswer
CASE pnTalk = 2
WAIT WINDOW SUBSTR(pcMyName,2) + ": " + cAnswer
ENDCASE
RETURN
ENDIF
* Call the parent class method
PERSON::Hello(pcMessage,pnTalk,pcMyName)
ENDPROC
ENDDEFINE
Working with Objects from the class
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.
SET PROCEDURE TO Person
oJim = CREATEOBJ("Person")
oJim.Contacts("Blue")
oJim.DyeHair("Black")
oJim.Measure("5 foot 8")
oJohn = CREATEOBJ("Person")
oJohn.Contacts("Green")
oJohn.DyeHair("Red")
oJohn.Measure("6 foot 2")
oJim.Converse("oJohn","oJim")
Important things to notice
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.