Understanding Objects and Classes Barb Ericson ericson@cc.gatech.edu Jan 2005 Georgia Institute of Technology
Georgia Institute of Technology Learning Goals What does object-oriented mean? What are objects? What are classes? Exercises for conceptually understanding objects and classes Scenarios Role-playing Georgia Institute of Technology
What is Object-Oriented? This means that we focus on the objects not just the procedures Focus on who (what objects?) as well as what (what do the objects do?) This contrasts with procedural programming in which we focus on the task that needs to be done. You may break that task up into smaller parts. In object-oriented programming we need to know who (what class) as well as what (procedures). Object-oriented programming is concerned with responsibility. Who (what object) is responsible for doing the job, not just what jobs need to be done. Georgia Institute of Technology
Georgia Institute of Technology What is an Object? A person, place, or thing That knows something about itself Has data (attributes, fields) A cashier has a id, name, and a password And can do something Has operations (methods) A cashier can total the items, take payment, make change Point out objects in the room (like a book, pen, computer, person, etc). Talk about what type of object it is, what data it has, what it can do. The picture is a cashier in a music store. The data is his name and password. The operations are how to “ring up” and “take payment”, and “make change”. Georgia Institute of Technology
Georgia Institute of Technology What is a Class? The type of an object The way we classify an object “The Idiot” by Dostoevsky is a book “War and Peace” by Tolstoy is a book Mary is a cashier Tasha is a cashier Grouping of objects with the same data and operations Objects are instances of a class. This slides shows two book objects of the class book. Georgia Institute of Technology
Georgia Institute of Technology Class: Example Mary is a cashier Tasha is a cashier Cashier is a class All cashiers have an id, name and password Each will have a different id, name, and password All cashiers can total an order, take payment, make change Georgia Institute of Technology
Georgia Institute of Technology Object Data Each object has its own data Tasha’s id is 4 and password is mhall Mary’s id is 3 and password is smile4 All cashier objects have an id, name, and password Changing Mary’s data won’t affect Tasha’s data Object data is also called instance variables, object variables, and object fields. Georgia Institute of Technology
Understanding Objects and Classes What “types” of objects are in the classroom? What data is known about each type and what operations can objects of that type do? Are there several objects of the same “type”. How are they the same and how different? There are probably computers in your room. What do we mean by a “computer”? What data does it have (manufacturer, size screen, size memory, etc). What can it do? Point out that nobody had to tell the students that these where computers. How did they know? They fit their idea of what a computer looks like. Georgia Institute of Technology
Georgia Institute of Technology Simulation Object-oriented development means creating a simulation of the problem We need to know the objects in the problem So we can create software objects to represent them We can’t put the “real” objects into the computer so we need to make software objects that represent the “real” objects. Georgia Institute of Technology
Classes Define the Objects The computer doesn’t know what we mean by a car or cashier We define the class Cashier so that the computer will understand what a cashier or bank account “is” and what it can “do” In the context of the problem we are trying to solve Then the computer can create objects to do the simulation Georgia Institute of Technology
Classes Create Objects The class can be thought of as a recipe, blueprint, or factory Many objects can be created from one class Objects keep track of the class that created them I am an object (instance) of the Cookie class When you create an object it has a reference to the object that represent the class that created it. So, objects always know what type (class) they are. Georgia Institute of Technology
Software Objects are Models The objects we create in software are models of the physical object We can’t stick a person in our software We can create a model of the person with the information we need to know for that person for our task Cashier id name password Georgia Institute of Technology
Using Scenarios to Identify Objects What objects are involved in going to the doctor because you are sick? What objects are involved in going to the movies? What objects are involved in a card game like blackjack? Ask people to walk through each of these scenarios. Then go back and see what objects they talk about or use. Bring a deck of cards with you and have two or more people play blackjack. Georgia Institute of Technology
Objects have Responsibilities An object-oriented design Determines the tasks to be done Determines what objects will be responsible for each task No one object does everything Objects work together to accomplish tasks The assignment of responsibilities is the key skill in object-oriented design Georgia Institute of Technology
Georgia Institute of Technology Identifying Classes What type of thing was each thing in the scenario? How would you classify it? Patient, Doctor, Nurse, Insurance … Movie, Ticket Seller, Ticket Taker, Ticket, Popcorn, Drink … Card, Deck, Player, Suit … How many objects of that types were in each scenario? What data did each object have associated with it? What could it do? What type of thing is each item? For example popcorn is a type of food (or snack). For each snack we would need to know at least the name and price. Georgia Institute of Technology
Learning about Objects and Classes Walk through scenarios Do role-playing Underline nouns in scenarios to identify the objects and classes Create CRC cards Draw UML diagrams Write methods and classes Specialize a parent class Use a debugger to step through execution Georgia Institute of Technology
Role Playing Exercises Page at http://web.sbu.edu/cs/dlevine/RolePlay/roleplay.html has links to exercises Powerpoint slides you can hand out developed by Joseph Bergin of Pace University FirstDayRolePlay-Bergin.ppt Georgia Institute of Technology
Georgia Institute of Technology Summary Object-oriented programs are simulations Objects simulate the “real world” and interact to accomplish a task Classes define the data all objects will have and what things they can do Ways to start understanding Objects and Classes Scenarios Role-Playing Georgia Institute of Technology