18
CSC 131 CSC 131 Fall 2006 Fall 2006 Lecture # 6 Lecture # 6 Object-Oriented Object-Oriented Concepts Concepts

CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Embed Size (px)

Citation preview

Page 1: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

CSC 131CSC 131

Fall 2006Fall 2006

Lecture # 6Lecture # 6

Object-Oriented ConceptsObject-Oriented Concepts

Page 2: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

The OO MindsetThe OO Mindset

problem domainproblem domain

objectsobjects

Page 3: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

ClasseClassess• Object-oriented thinking begins with the definition Object-oriented thinking begins with the definition

of a class often defined as:of a class often defined as:

– templatetemplate

– generalized descriptiongeneralized description

– “ “blueprint” ... describing a collection of similar blueprint” ... describing a collection of similar itemsitems

• A metaclass (also called a superclass) is a collection A metaclass (also called a superclass) is a collection of classes.of classes.

Page 4: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

What Is a Class?What Is a Class? A class is a description of a set of objects that share the A class is a description of a set of objects that share the

same same attributesattributes, , operationsoperations, , relationshipsrelationships, and , and semantics. semantics.

– An object is an instance of a class.An object is an instance of a class.

A class is an abstraction in that itA class is an abstraction in that it– Emphasizes relevant characteristics.Emphasizes relevant characteristics.– Suppresses other characteristics.Suppresses other characteristics.

Page 5: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

What is a What is a Class?Class?

external entities

things

occurrences roles

organizational units

places

structures

class name

attributes:

operations:

Page 6: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

What is an Object? What is an Object? Objects are key to understanding Objects are key to understanding object-orientedobject-oriented

technology. technology.

Many examples of real-world objects: your desk, Many examples of real-world objects: your desk, your television set, your bicycle. your television set, your bicycle.

These real-world objects share two characteristics: These real-world objects share two characteristics: statestate and and behaviorbehavior..

Page 7: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

ObjectsObjectsSoftware objects are modeled after real-world objects Software objects are modeled after real-world objects

in that they too have in that they too have state and behavior. state and behavior.

A software object maintains its state in one or more A software object maintains its state in one or more variablesvariables . .

A software object implements its behavior with A software object implements its behavior with methods.methods.

A method is a function (subroutine) associated with A method is a function (subroutine) associated with an object. an object.

Page 8: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Object TypesObject TypesExternal entities: sensors, actuators, control panel, External entities: sensors, actuators, control panel,

devicesdevices

Information items : displays, commands, etc.Information items : displays, commands, etc.

Entities which establishes the context of the Entities which establishes the context of the problem : controller, monitors, schedulersproblem : controller, monitors, schedulers

Page 9: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Identifying ClassesIdentifying ClassesAn object may appear as a noun (ex. An object may appear as a noun (ex.

Measurement) or disguised in a verb (to measure)Measurement) or disguised in a verb (to measure)

A method might appear as a verb (ex. Investigate) A method might appear as a verb (ex. Investigate) or disguised in a noun (investigation)or disguised in a noun (investigation)

Attributes describe some kind of characteristics Attributes describe some kind of characteristics for the object (adjectives). for the object (adjectives).

Page 10: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Criteria for Evaluating Candidate ClassesCriteria for Evaluating Candidate Classes

One or more attributesOne or more attributes

Needed functionality (one or more Needed functionality (one or more methods)methods)

Common attributes (apply to all objects Common attributes (apply to all objects of a specific class)of a specific class)

Common functionality (apply to all Common functionality (apply to all objects of a specific class)objects of a specific class)

Page 11: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Building a Building a ClassClass

class name

attributes:

operations:

attributes:

operations

Page 12: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Encapsulation/Encapsulation/HidingHidingThe object encapsulates

both data and the logicalprocedures required tomanipulate the data

Achieves “information hiding”

method # 1

data

method # 2

method # 4

method # 5

method # 6

Page 13: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Class Class HierarchyHierarchy

chairtable desk

instances of chair

furniture (superclass)

subclasses of thefurniture superclass

Page 14: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

MethodsMethods(a.k.a. Operations, (a.k.a. Operations,

Services)Services)An executable procedure that is encapsulated in a class and is designed to operate on one or more data attributes that are defined as part of the class.

A method is invoked via message passing.

Page 15: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Encapsulation, Inheritance, and PolymorphismEncapsulation, Inheritance, and Polymorphism A class A class encapsulatesencapsulates data and operations that manipulates the data and operations that manipulates the

data in a single packagedata in a single package

Benefits of encapsulation:Benefits of encapsulation:

– implementation details are hidden (information implementation details are hidden (information hiding)- reduces the propagation of side effects hiding)- reduces the propagation of side effects when changes occur.when changes occur.

– Data structure and operations are in a single entity Data structure and operations are in a single entity (class) – component reuse(class) – component reuse

– Object interfaces are simplified – by message Object interfaces are simplified – by message passing and objects are not concerned with the passing and objects are not concerned with the details of internal data structures.details of internal data structures.

Page 16: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

InheritanceInheritance It is a key concept in OO paradigmIt is a key concept in OO paradigm

A subclass inherits all of the attributes and A subclass inherits all of the attributes and operations associated with its superclass.operations associated with its superclass.

It differentiates between conventional and OO It differentiates between conventional and OO development.development.

This implies that all data structures and This implies that all data structures and algorithms that implemented for superclass are algorithms that implemented for superclass are available for the subclass- available for the subclass- Reuse is Reuse is accomplished.accomplished.

Page 17: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

PolymorphismPolymorphism

One operation – different resultsOne operation – different results Reduces designReduces design Reduces effort to extend the systemReduces effort to extend the system

Page 18: CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts

Questions ….Questions ….