Introduction to Object Oriented Programming
| This page has been nominated for cleanup for the following reason: Content is a copy of some other resource, not formatted for Wikiversity..
Please edit this page to improve it. See this module's talk page for discussion.
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 resumé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.
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 ObjectsEdit
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:
- The properties that describe the object,
- The methods , or actions, that you wish to associate with the object.
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:
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.
Properties of "Person" object Issy Name: Issy Height: 59" Hair Color: Blonde Build: Petite Glasses: Yes Clothing: Business casual Shoes: Black Accessories: Tan leather briefcase Gender: Female Methods: CarrySign(), Speak(), Walk(), WaveHands()
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 in our list. 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.
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 and manipulate the data contained within the object.
We can depict the phone conversation between Issy and Jack as objects of the "Person" class. 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().
In a way a class may be viewed as a specific type of 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.