Introduction to Object Oriented Programming

From Wikiversity

Jump to: navigation, search

In this chapter, you will learn: What an object is What the term state means with respect to objects What a class is What it means to instantiate an object What properties are What methods are How to use some of the objects provided by Visual Studio .NET Understanding Objects Suppose you are the personnel manager for a company and you need to hire someone to fill an important position. After sifting through dozens of r é sum é s, you select one candidate to call for a face - to - face interview at your company offices. You call her (let ’ s say her name is Issy) on the phone and chat for a few minutes and confirm that she appears to be the right person for the job. You (we ’ ll pretend your name is Jack) make arrangements for Issy to fly to your location, stating that you will meet her at the airport. Figure 2 - 1 shows arranging a job interview.




However, since the two of you have never met before, you start asking a few questions so you can recognize each other at the airport. Issy says she ’ s short with blonde hair and that she will be wearing a black business suit and carrying a tan leather briefcase. You then describe yourself as six feet tall with brown hair and say that you ’ ll be wearing a gray suit. You then set a date and time for the flight and everything ’ s ready for the interview. Everyday Use of Objects Perhaps without realizing it, both of you used objects in the course of your conversation. (An object is just a simplification of something that you wish to use in your program. In this example, you are creating a list of properties that will be used to describe a person object.) First, you implicitly created a person class during the phone call. A class is a template used to describe an object. As such, a class is an abstraction or simplification of some object you observe in the real world. You can break a class down into two basic components: 1. those properties that describe the object, and 2. those methods , or actions, that you wish to associate with the object. Class Properties The class properties are the data that you want to record and associate with an object. If you wanted to create a class person object, a list of properties might include those shown in Table 2 - 1 . Candidate Recruiter Figure 2 - 1 Table 2-1: clsPerson Properties name gender height build hairColor eyeColor clothing accessories



It ’ s important to notice that, prior to the phone conversation, the properties list for the class person named Issy is virtually empty. In fact, all you were able to fill in from her r é sum é were her name and gender. However, after the phone conversation you were able to fill in almost all of the properties for the class person object named Issy . (You might scare her away if you tried to fill in the Build and eyeColor properties over the phone.) While you were filling in a class person object named Issy , she was doing the same thing for a class person object named Jack . Prior to the phone call, the class person object Issy created to be associated with the name Jack may have been totally empty, because Issy had no idea who might be calling her about a job interview. However, the dialog on the phone enabled each party to fill in at least some of the property values for the other. From Issy ’ s point of view, her class person object went from a totally nondescript object to (at least) a partially identifiable object after the phone call was completed. By changing the values of the class properties, you are able to change the state of the object. The state of an object is determined by the values of the properties used to describe the object. In our example, the properties used to describe the state of a class person object are those shown in Table 2 - 1 . While people don ’ t change their names very often, it happens occasionally. Likewise, people do gain and lose weight, dye their hair, wear tinted contacts, change clothes, and alter their accessories. If any of these property values change, the state of the object also changes. Just keep in mind that anytime the value of a property changes, the state of the object — by definition — also changes. Class Methods Just as there are property values that define the state of an object, there are usually class methods that act on the properties. For a class person object, you would want that object to be able to talk, wave his or her arms, walk, change clothes, and so forth. In short, the class methods determine the behaviors the object is capable of performing. Methods are used to describe whatever actions you wish to associate with the object. Methods often are used to manipulate the data contained within the object. We can depict the phone conversation between Issy and Jack as objects of the person class as shown in Figure 2 - 2 . Often, class methods are used to take one or more property values, process the data those properties contain, and create a new piece of data as a byproduct of the method ’ s process. For example, you might create an invoice object that has priceEach and quantityOrdered (among others) as properties of an Invoice class. You might then create a method named salesTaxDue() as a class method that would compute the sales tax due for the invoice. In fact, you might have another Invoice property named salesTax that gets filled in automatically as part of the code contained in the method named salesTaxDue() .





Properties: Name: Hight: Hair Color: Build: Glasses: Clothing: Shoes: Accessories: Gender: Methods: CarrySign() Speak() Walk() WaveHands() Values Issy 59" Blonde Petite Yes Business casual Black Tan leather briefcase Female Person Object for Issy




Properties: Name: Hight: Hair Color: Build: Glasses: Clothing: Shoes: Accessories: Gender: Methods: CarrySign() Speak() Walk() WaveHands() Values Jack 72" Sandy Blonde Overweight No Black business suit,



If you think about it, a class property may be viewed as a noun: a person, place, or thing. Class methods, on the other hand, often behave like verbs, denoting some kind of action to be taken on the data. White shirt, red tie Black Overcoat Male

  • Properties:
  • Name:

Hight: Hair Color: Build: Glasses: Clothing: Shoes: Accessories: Gender: Methods: CarrySign() Speak() Walk() WaveHands() Values Issy 59" Blonde Petite Yes Business casual Black Tan leather briefcase Female Person Object for Issy Person Object for Jack