CS302 Topic: Intro. to Object Oriented Pgmg. Tuesday, Sept. 6, - - PowerPoint PPT Presentation

cs302 topic intro to object oriented pgmg
SMART_READER_LITE
LIVE PREVIEW

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!!!


slide-1
SLIDE 1

CS302 Topic: Intro. to Object Oriented Pgmg.

Tuesday, Sept. 6, 2005

slide-2
SLIDE 2

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 ☺

slide-3
SLIDE 3

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
slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Basic Principles of Object Orientation

Abstraction Encapsulation Modularity Hierarchy Inheritance

slide-8
SLIDE 8

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

slide-9
SLIDE 9

What is Encapsulation?

Hide implementation from clients

  • Clients depend on interface
slide-10
SLIDE 10

What is Modularity?

The breaking up of something complex into manageable pieces or modules Queue Canteen System Order Placement Delivery Billing

slide-11
SLIDE 11

What is Hierarchy?

Level of abstraction Increasing abstraction

Art Music Films Sculptures R&B Rap Sci-Fi Action …

Decreasing Abstraction

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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
slide-15
SLIDE 15

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

slide-16
SLIDE 16

More detailed look at Object Orientation…

  • 1. Object
  • 2. Class
  • 3. Attributes
  • 4. Operation
  • 5. Interface (Polymorphism)
  • 6. Generalization Relationships
slide-17
SLIDE 17
  • 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

slide-18
SLIDE 18

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
slide-19
SLIDE 19

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:

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Example: Objects

English 102 CS102 Math 241 CS 140 Physics 135 CS302 CS 311

slide-23
SLIDE 23
  • 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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

Representing Classes

As an example, we can represent a class using a compartmented rectangle

Professor

slide-27
SLIDE 27

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()

slide-28
SLIDE 28

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

slide-29
SLIDE 29

Example: Class

English 102 CS102 Math 241 CourseOffering CS 140 Physics 135 CS302 CS 311

slide-30
SLIDE 30

Classes of Objects

How many classes can you see?

slide-31
SLIDE 31
  • 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

slide-32
SLIDE 32
  • 4. What is an Operation?

CourseOffering

addStudent deleteStudent getStartTime getEndTime

Operations Class

slide-33
SLIDE 33
  • 5. What is Polymorphism?

The ability to hide many different implementations behind a single interface

Manufacturer A Manufacturer B Manufacturer C

OO Principle: Encapsulation

slide-34
SLIDE 34
  • 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

slide-35
SLIDE 35
  • 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

slide-36
SLIDE 36

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

slide-37
SLIDE 37

Multiple Inheritance

A class can inherit from several other classes

FlyingThing Animal Airplane Helicopter Bird Wolf Horse

multiple inheritance

slide-38
SLIDE 38

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

slide-39
SLIDE 39

Example: What Gets Inherited

GroundVehicle

weight licenseNumber register()

Car

size getTax()

Truck

tonnage Superclass (parent) Generalization Subclasses

slide-40
SLIDE 40

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

slide-41
SLIDE 41

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
slide-42
SLIDE 42

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

slide-43
SLIDE 43

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
slide-44
SLIDE 44

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
slide-45
SLIDE 45

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

slide-46
SLIDE 46

A simple Sales Order Example

Order Product Ship via

slide-47
SLIDE 47

Class Diagram for the Sales Example

Sale Salesperson Customer Vehicle Product Corporate Individual Truck Train

buyer Item sold Shipping mechanism seller

slide-48
SLIDE 48

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 …

slide-49
SLIDE 49

☺ Movie Case Study: Creating Swarms (and relationship to OOP) ☺

Movies…

slide-50
SLIDE 50

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
slide-51
SLIDE 51

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
slide-52
SLIDE 52

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

slide-53
SLIDE 53

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)
slide-54
SLIDE 54

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