What is OOP Humans organize their environment using

























- Slides: 25
What is OOP? • Humans organize their environment using objects – An object is something that can be perceived by the senses • Humans interact with objects to make things happen – I push the cat off the couch so I can sit down – I press on the brake to stop my car 9/30/2020 CS 1 - OOP 1
Computers • Computers focus on the sequence of steps required to complete a given task – Given a recipe the computer follows it exactly • Computers view their environment as a set of procedures • Humans on the other hand view their environment as a collection of objects 9/30/2020 CS 1 - OOP 2
OO View 9/30/2020 CS 1 - OOP 3
Procedural View 1. Obtain the current temperature of the room 2. Obtain the current setting on the dial 3. If the room temperature is less than the setting on the dial, turn on the heater 4. If the room temperature is greater than or equal to the setting on the dial, turn off the heater 5. Go back to step 1 9/30/2020 CS 1 - OOP 4
The Early Days • When programming languages were first developed they modeled what the computer did – Procedural languages: FORTRAN, COBOL, ALGOL, Pascal, … • Why go procedural? – Computer language design was in its infancy – Compilers were very primitive 9/30/2020 CS 1 - OOP 5
Simula • In 1962, Dahl and Nygaard, started work on the first object based-based programming language – Simula • Simula was designed for discrete event simulation and focused on modeling rather than general purpose computing • Simula was the first programming language that allowed programmers to describe computational constructs using objects 9/30/2020 CS 1 - OOP 6
Alan Kay • Worked at Xerox Parc • Intrigued by Simula – “promise of an entirely new way to structure computations” • Developed Smalltalk which became one of the first commercially available OO languages 9/30/2020 CS 1 - OOP 7
Bjarne Strousoup • Worked with Simula while working on his Ph. D • Went to work for Bell Laboratories. – Felt that the ideas in Simula would be useful in his work • Developed C++ 9/30/2020 CS 1 - OOP 8
Objects • The term object is not easily defined • According to Webster: – Object: a visible or tangible thing of relative stable form; A thing that may be apprehended intellectually; A thing to which thought or action is directed • In this class, we will use the following definition: – An object has state, behavior, and identity (Booch) 9/30/2020 CS 1 - OOP 9
State • The state of an object encompasses all of the (static) properties of the object plus the current (dynamic) values of each of these properties • A property is an inherent or distinctive characteristic, trait, quality, or feature that contribute to making an object uniquely that object • We will use the word attribute, or data member, to refer to the state of an object 9/30/2020 CS 1 - OOP 10
Behavior • Behavior is how an object acts and reacts, in terms of state changes and interactions with other objects. • An operation is some action that one object performs upon another in order to elicit a reaction. • We will use the word method to describe object behavior. • Invoking a method causes the behavior to take place. 9/30/2020 CS 1 - OOP 11
Identity • Is a river still the same river from one day to the next, even if the same water never flows through it? 9/30/2020 CS 1 - OOP 12
Identity • Identity is the property of an object that distinguishes it from all other objects. • The failure to recognize the difference between the name of the object and the object itself is the source of many errors in object-oriented (OO) programming. 9/30/2020 CS 1 - OOP 13
Assignment and Equality • What does it mean to assign one object to another? – Copy the name only (shallow copy) – Duplicate the object, creating a different object (with a different name) whose state and behavior is the same as the original (deep copy) • Equality like assignment, can mean two things – Two names designate the same object – Two objects are different but their state and behavior are the same 9/30/2020 CS 1 - OOP 14
Name Equivalence • Two variables are said to be name-equivalent if they both refer to the exact same object. • When comparing objects by name we only interested in determining if two objects are in fact the exact same object. • In this “shallow” way of identifying objects, two objects are considered to be the same if they occupy the exact same memory locations. – That is, they are the exact same object. 9/30/2020 CS 1 - OOP 15
Content Equivalence • Two objects are said to be content-equivalent if. – The objects are both from the same class, – And the state information stored in each object is exactly the same. • To determine content-equivalence you must understand the structure of the objects being compared and determine if the state of both objects are exactly the same. 9/30/2020 CS 1 - OOP 16
Example T 1 T 2 T 3 Thermostat State: Desired. Temp: Current. Temp: Heater. State: 65 66 on Desired. Temp: Current. Temp: Heater. State: off Behavior: Set. Desired. Temp Turn. Heater. On Turn. Heater. Off 9/30/2020 T 4 Set. Desired. Temp Turn. Heater. On Turn. Heater. Off CS 1 - OOP 75 83 Desired. Temp: Current. Temp: Heater. State: 65 66 on Behavior: Set. Desired. Temp Turn. Heater. On Turn. Heater. Off 17
Object-Oriented Design • Traditional design mechanisms are control based – Flow charts!! • OOD is object-based – Identify the objects that make up the system – Define the state and behavior of these objects – Determine the relationships between these objects 9/30/2020 CS 1 - OOP 18
Objects and Classes • A class is an abstract definition of an object. – It defines the structure and behavior of an instance (object) in the class. – It serves as a template for creating objects. • When doing OOP you define classes, from which objects are instantiated. • When doing OOD, although we focus on objects, we are really interested in finding classes. 9/30/2020 CS 1 - OOP 19
Finding Classes • A class should capture one and only one key abstraction. – Student class which represents a student and their schedule for the current quarter. – Student and schedule classes. • The student class would include a reference to an instance of a schedule as part of its state. • A class should represent something as opposed to doing something. 9/30/2020 CS 1 - OOP 20
Naming Classes • A class name is typically a single noun that best characterizes the abstraction. • Difficulty in naming a class may be an indication of a poorly defined abstraction. • Names should come directly from the vocabulary of the domain. – Student, Schedule, Course, College, … 9/30/2020 CS 1 - OOP 21
Define Class Semantics • After naming a class, a brief concise description of the class should be made. – Focus on the purpose of the class not on the implementation. • The class name and description form the basis for a model of the system. • Look for the whats and ignore the hows. 9/30/2020 CS 1 - OOP 22
Finding Classes • Classes are often identified by examining the nouns and noun phrases in the description of a system. • Nouns found may be: – Classes/objects. – Descriptions of state of an object. – External entities (more on this later). – None of the above. 9/30/2020 CS 1 - OOP 23
Filtering Nouns • When identifying nouns, be aware that: – Several terms may refer to the same object. – One term may refer to more than one object. – Natural language is ambiguous. • This approach can identify many unimportant or non-objects: – The list must be filtered. 9/30/2020 CS 1 - OOP 24
Looking at Nouns • The following requirement was written for a banking system. – “Legal requirements shall be taken into account. ” • What will be the result if only nouns are considered? • Each noun must be considered in the context of the problem domain. – Nouns cannot stand by themselves. 9/30/2020 CS 1 - OOP 25