CS302 Topic: Intro. to Object Oriented Pgmg. Tuesday, Sept. 6, - - PowerPoint PPT Presentation
CS302 Topic: Intro. to Object Oriented Pgmg. Tuesday, Sept. 6, - - PowerPoint PPT Presentation
CS302 Topic: Intro. to Object Oriented Pgmg. Tuesday, Sept. 6, 2005 Announcements Lab 1 due this Friday Lab 2 is now available (online) (due Friday, Sept. 16) Uses red-black trees Still in C Dont procrastinate!!!
Announcements
- Lab 1 due this Friday
- Lab 2 is now available (online) (due Friday, Sept. 16)
- Uses red-black trees
- Still in C
- Don’t procrastinate!!!
Finish Lab 1 early, then get early start on Lab 2.
- This week’s lab meetings will discuss Lab 2 assignment
- ☺ Today’s m ovie case study:
Movies of swarms, and relationship to OOP ☺
Computational Models
Modeling is an essential part of many disciplines in science and engineering, including software engineering The importance of having a “good” model increases with the complexity
- f the project
Computational Models
A model is a simplification of reality Main reason for developing models:
- Understanding the system
being developed
We build models of complex systems because we cannot comprehend such a system in its entirety
Principles of Modeling
The choice of the model has a significant impact on how the problem is approached and how a solution is devised A model can be expressed at different levels
- f precision
A good model is connected to reality A single model is not sufficient to represent most systems; A set of nearly independent models are required
Models in Software Engineering
A modeling technique suitable for the programming approach should be used Since object-oriented approach has proven to be superior to other approaches, models designed for this approach are widely used in software engineering
Basic Principles of Object Orientation
Abstraction Encapsulation Modularity Hierarchy Inheritance
What is Abstraction?
Counter Queue Items An example of an item purchasing abstraction: A model that includes most important aspects
- f a given problem while ignoring less
important details
What is Encapsulation?
Hide implementation from clients
- Clients depend on interface
What is Modularity?
The breaking up of something complex into manageable pieces or modules Queue Canteen System Order Placement Delivery Billing
What is Hierarchy?
Level of abstraction Increasing abstraction
Art Music Films Sculptures R&B Rap Sci-Fi Action …
Decreasing Abstraction
What is Inheritance?
Process by which one object acquires properties
- f another object
Supports concept of hierarchical classification With inheritance, object only needs to define what makes it unique within its class In OOP, Inheritance means inheriting another
- bject’s interface, and possibly its
implementation
Example of Interface Inheritance
- Suppose you want to define Search_Object_1 that:
- Stores (key, value) pairs
- Allows values to be looked up using keys
- Supported operations for Search_Object_1:
- insert: Adds a (key, value) pair to the object
- delete: Deletes a (key, value) pair from the object
- lookup: Given a key, retrieves the value associated with that key from
the object
- Later, define new object (Search_Object_2) that additionally
allows traversing (key, value) pairs in sorted order
- Supported operations for Search_Object_2:
- All of above, plus:
- rewind: returns us to beginning
- next: returns next (key, value) pair
- Accomplish this by having Search_Object_2 inherit
Search_Object_1’s interface
Example of Implementation Inheritance
Suppose:
- Search_Object_1 is implemented using binary
search tree
- You already have a binary search tree
implemented, including implementations for the previous operations, but using different names
To inherit binary search tree implementation, we make Search_Object_1 be a subclass of the binary tree.
- We then make the insert, delete, and lookup
- perations call the appropriate binary tree
- perations
But, there’s a problem with Implementation Interface…
Remember:
- Object is supposed to hide its implementation
- Implementation should be interchangeable with other
- bjects that implement the same interface
Problem:
- Search object’s interface is tied to the binary search
tree’s interface
- By making search object inherit from binary search
tree, we’ve also made its implementation dependent
- n the binary tree
NOT GOOD!!
Nearly always best to only inherent interface
More detailed look at Object Orientation…
- 1. Object
- 2. Class
- 3. Attributes
- 4. Operation
- 5. Interface (Polymorphism)
- 6. Generalization Relationships
- 1. What is an Object?
An object is a “smart” data structure
- Set of state variables
- Set of methods for manipulating state variables
- Examples – physical, conceptual, or software entities:
- Physical entity:
- Conceptual entity:
- Software entity:
robot chemical process linked list data structure
Objects (con’t.)
An object advertises:
- The types of data it will store
- The types of operations it allows to manipulate
its data (i.e., its interface)
An object hides:
- Its implementation of the above
An object is something that has:
- State
- Behavior (i.e., operations)
- Identity
An Object Has State
The state of an object is one of the possible conditions in which an object may exist The state of an object normally changes over time Represented by: Attribute values + Links (relationship instances)
Object:
- L. Parker
- L. Parker
9738239
- Aug. 1, 2002
Teaching Name: Employee ID: Date hired: Status:
An Object Has Behavior
Behavior determines how an object acts and reacts to requests from other objects Behavior is represented by the set of messages it can respond to (i.e., the operations the
- bject can perform)
Add me to CS302 with L. Parker (Returns: confirmation) Registration System CS302 Course
Representing Objects
As an example, we can represent an object as rectangles with underlined names
: Professor
- L. Parker
Class Name Only Object Name Only
- L. Parker :
Professor
Class and Object Name
Example: Objects
English 102 CS102 Math 241 CS 140 Physics 135 CS302 CS 311
- 2. What is a Class?
A class is a description of a group of objects with common properties (attributes), behavior (operations), relationships, and semantics
An object is an instance of a class
A class is an abstraction in that it:
Emphasizes relevant characteristics Suppresses other characteristics
The Relationship Between Classes and Objects
A class is an abstract definition of an object
- It defines the structure and behavior of each
- bject in the class
- It serves as a template for creating objects
Objects may be grouped into classes
- A particular object of a class is an instance
Professor
Professor Thomason Professor Berry Professor Parker
Sample Class
- Class
Course Properties Name Location Days offered Credit hours Start time End time Behavior Add a student Delete a student Get course roster Determine if it is full
Representing Classes
As an example, we can represent a class using a compartmented rectangle
Professor
Class Compartments
For example, we can represent a class as being comprised of three sections:
- The first section contains the class name
- The second section shows the structure
(attributes)
- The third section shows the behavior
(operations)
Professor
Name save() change() delete() empID create()
Class Compartments (cont.)
The second and third sections may be suppressed if they need not be visible on the diagram
Professor Professor
save() change() delete() create()
Professor Professor
Name save() change() delete() empID create() Name empID
Professor
Example: Class
English 102 CS102 Math 241 CourseOffering CS 140 Physics 135 CS302 CS 311
Classes of Objects
How many classes can you see?
- 3. What is an Attribute?
Object Class Attributes
CourseOffering
Number startTime endTime
:CourseOffering
Number=CS311 endTime=1655 startTime=1540
Attribute values
:CourseOffering
Number=CS302 endTime=1525 startTime=1410
- 4. What is an Operation?
CourseOffering
addStudent deleteStudent getStartTime getEndTime
Operations Class
- 5. What is Polymorphism?
The ability to hide many different implementations behind a single interface
Manufacturer A Manufacturer B Manufacturer C
OO Principle: Encapsulation
- 5. (con’t.) What is an Interface?
A named set of operations that characterize the behavior of an element.
- The interface formalizes polymorphism
<<interface>> Polygon
scale move rotate draw
Triangle Square Pentagon
- 6. Relationships: Generalization
A relationship among classes where one class shares the structure and/ or behavior of one or more classes Defines a hierarchy of abstractions in which a subclass inherits from one or more super classes
Single inheritance Multiple inheritance
Generalization is an “is-a-kind of” relationship
Example: Single Inheritance
One class inherits from another
Ancestor
BankAccount
balance number name Withdraw() CreateStatement()
Current
Withdraw()
Savings
GetInterest() Withdraw() Superclass (parent) Generalization Relationship Subclasses Descendents
Multiple Inheritance
A class can inherit from several other classes
FlyingThing Animal Airplane Helicopter Bird Wolf Horse
multiple inheritance
What Gets Inherited?
A subclass inherits its parent’s attributes and
- perations
A subclass may:
- Add additional attributes or operations
- Redefine inherited operations (use caution)
Common attributes, operations, and/ or relationships are shown at the highest applicable level in the hierarchy
Example: What Gets Inherited
GroundVehicle
weight licenseNumber register()
Car
size getTax()
Truck
tonnage Superclass (parent) Generalization Subclasses
Strengths of Object Orientation
Facilitates architectural and code reuse Models more closely reflect the real world
More accurately describe corporate data and processes Are decomposed based on natural partitioning Can be easier to understand and maintain
Stability
A small change in requirements does not mean massive changes in the system under development
Contrast 2 Programming Styles: (1) Procedural and (2) Object-Oriented
Procedural programming:
- Organize system around procedures that
- perate on data
Object-oriented programming
- Organize system around objects that receive
messages
- An object encapsulates data and operations
Computation as Simulation
Procedure programming consists of procedures acting on data Object-oriented programming consists of
- bjects interacting
Main() creates a web of objects and starts them interacting
The Object Oriented Programming Process
Step 1:
- Identify the data and the operations on the data
- These begin to form the classes
Step 2:
- Determine how the classes interact
(Iterate) Step n:
- Create program on top of the classes
Using classes and instances to design a system
- Suppose we want to build a “star trek wars” simulator
- We can begin by thinking about what kinds of objects we
want (i.e., what classes, their state information, and their interfaces)
- Ships
- Planets
- Foreign creatures
- We can then extend to thinking about what particular
instances of objects are useful
- Millenium Falcon
- Enterprise
- Earth
Further extensions to our simulator…
Animate the world:
- Add a clock that moves time forward in the
universe
- Keep track of things that can move
- Clock sends “CLOCK-TICK message” to objects
to have them update their state
Add TORPEDO class to system
A simple Sales Order Example
Order Product Ship via
Class Diagram for the Sales Example
Sale Salesperson Customer Vehicle Product Corporate Individual Truck Train
buyer Item sold Shipping mechanism seller
Effect of Requirements Change
Sale Salesperson Customer Vehicle Product Corporate Individual Truck Train
seller buyer Item sold Shipping mechanism
Airplane
Suppose you need a new type of shipping vehicle …
☺ Movie Case Study: Creating Swarms (and relationship to OOP) ☺
Movies…
How much do you know? Let’s quiz you…
What concept does each of the following refer to?
- “Abstractions arranged in order of rank or level”
- ANSWER: Hierarchy
- “The breaking up of something complex into
manageable pieces”
- ANSWER: Modularity
- “Extracting the essential details about an item or
group of items, while ignoring the unessential details”
- ANSWER: Abstraction
- “Enclosing all parts of an abstraction within a
container”
- ANSWER: Encapsulation
How much do you know? Let’s quiz you…
What concept does each of the following refer to?
- “Process by which one object acquires properties of
another object”
- ANSWER: Inheritance
- “The ability to hide many different implementations
behind a single interface”
- ANSWER: Polymorphism
- “An instance of a class”
- ANSWER: An object
History and Languages
- 1967
Simula
- 1970-83
Smalltalk
- 1979
Common LISP Object System
- 1980
Stroustrup starts on C+ +
- 1983
Objective C
- 1986
C+ +
- 1987
Actor, Eiffel
- 1991
C+ + release 3.0
- 199x
Plethora of OOP books/ articles
- 1996
Java
- 1983-89
Language books with OOP concepts
- 1989-92
Object-oriented design books
- 1992-present
Object-oriented methodology books
OO Programming Languages
- We’ll be using C+ + , but there are lots of other object-
- riented languages:
- Java
- Self
- Python
- Perl
- Prograph
- Modula 3
- Oberon
- Scheme
- Smalltalk Venders
- Prolog+ +
- Ada 95
- Object Pascal (Delphi)
- …
That’s all, folks…
- Your homework:
- Keep working on Lab 1!!
Due Friday, Sept. 9.
- If you haven’t already, read handout for today on Object-
Oriented Programming
- See “Schedule/ Readings/ Notes” link for today:
http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html
- Before class next time:
- 3 readings for next class
- See “Schedule/ Readings/ Notes” link for today:
http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html