Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a...

22
Chapter 4 Basic Object-Oriented Concepts

Transcript of Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a...

Page 1: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Chapter 4

Basic Object-Oriented Concepts

Page 2: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Chapter 4 Objectives

• Class vs. Object• Attributes of a class• Object relationships• Class Methods (Operations)• Encapsulation of Attributes and Methods• Messages and Message Sending• Polymorphism• Inheritance• Generalization / Specialization Hierarchies

Page 3: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Class vs. Object

• A class is a type of thing• All specific things that fit the general definition of the

class are things that belong to that class.• An object is an instance of a class, a specific thing. • For instance: class = dog, object = Fido• A class is much like the entity type used when

modeling data. • A data entity type is a general category of data.• An instance of an entity type is data about one

specific thing. – i.e. a specific customer

Page 4: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

• Each object in a class should be identifiable in some way so we can tell which object is which, uniquely.

• Usually one attribute or piece of information about an object is used to identify an object in the user’s work context. – primary key (similar to databases)

• When modeling data, an identifier is also required.• Sometimes the object naturally has an identifier, such

as social security number for a person. • An object in a computer system has an additional

identifier called an object reference.• The computer creates an object reference for each

object when the object is created.

Class vs. Object

Page 5: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

• All objects belong to a class of objects.– Fido and Bowser are objects that belong to class dog.

• Consider, for example, the interface objects– buttons and windows are specific things– button objects belong to a class named button– window objects belong to a class named window

• All window objects have the same attributes and behavior.

• When we interact with a specific window, we are interacting with an object.

• When we talk about windows generally we talk about a class.

• Same goes for buttons.

Class vs. Object

Page 6: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Class, data entities, relational tables

Page 7: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Attributes of a Class• An attribute is a piece of information that needs to be

known about the objects in a class. • Each object might a have different value for the

attribute. – one dog is named Fido, the other is named Bowser.

• Attribute values are part of what an object “knows” when you use the “object think” approach.

• Attributes of a class of objects are equivalent to attributes of a data entity type.

• An attribute can also be one of several data types.– Boolean, floating point, character, data structure, etc

Page 8: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Classes and Objects

Page 9: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Object Relationships

• An object might be naturally related to another object.• Object relationships are much like relationships in a

data model. • A relationship is an association based on the context

in which we view the objects.• For example, a dog might be associated with an

owner in the work context of the veterinarian.• An owner might be associated with many dogs.• The nature of an object relationship can also be

described very precisely, just as in data modeling.

Page 10: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

• A relationship can be optional or mandatory.• An optional relationship means that an object can be

associated with another object.– a rock might be associated with one shelf

• A mandatory relationship means that an association must exist.– a motor must be associated with a functional automobile

• The other aspect of a relationship that is same is as in data modeling is the cardinality of a relationship.

• Cardinality refers to the number of associations that naturally occur between objects.

• UML uses the term multiplicity in place of cardinality.

Object Relationships

Page 11: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

• There are three types of object relationships– association relationships– whole-part relationships– generalization-specialization relationships

• Association relationships means that one object is naturally associated with other objects in some way.

• Whole-part relationships means that the relationship between objects is stronger.

• Generalization-specialization relationships are hierarchies of types– dog, breed, hair color, etc

Object Relationships

Page 12: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Whole / PartC

om

pon

en

ts

Ag

gre

gati

on

Part

Wh

ole

Su

per

Cla

ss

Su

b C

lass

Page 13: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Generalization / Specialization

Source: http://people.cs.vt.edu/~kafura/cs2704/oop.swe.html

Sp

ecia

liza

tion

Gen

era

liza

tion

Su

per

Cla

ss

Su

b C

lass

Page 14: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Aggregation and Composition

• Two types of whole-part relationships– aggregation

– composition (strongest relationship)

• Aggregation– weak containment

– object may contain certain other objects

• Composition– strong containment

– object must contain certain other objects

Page 15: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Class Methods (Operations)

• A method is something an object “knows” how to do– behavior or responsibility of the object

• Two types of methods– standard methods– custom methods

• All classes of objects can do a few basic things– standard methods– i.e. create themselves and delete themselves

• Methods can be designed uniquely for a specific class of objects. – custom methods– implement the responsibilities of the objects that the system

analyst needs to identify in the problem domain of the user

Page 16: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Encapsulation of Attributes & Methods• Encapsulation

– several items are packaged together into one unit.

• With objects, both attributes and methods of the class are packaged together (encapsulated).

• An object knows things (attributes) and knows how to do things (methods).

• Makes the object much more than a data entity.– data entity just has attributes

• Encapsulation means a “cover” or “coating” that hides the internal structure and state of the object from the outside environment. – Other objects and end-users are prevented from doing

anything to or knowing anything about object’s insides.– “information hiding”

Page 17: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Messages & Message Sending• Message sending is another key concept.• When we interact with objects, we send messages to

objects and those object send messages to us or to each other.

• When we ask an object to do something we are sending a message to the object, asking it to invoke a method.

• Messages might be thought of as an input or output. • Messages sent to an object must correspond to a

method of the object. – We can ask an object to do what it knows how to do.– What an object knows how to do are its methods.

Page 18: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Polymorphism

• Closely related to message sending is Polymorphism– literally means “multiple forms”

• Ability to send a message to different receivers– message triggers correct method in each receiving object– greatly simplifies implementation of message sending

• Polymorphism allows different objects to respond appropriately to the same command.– greatly reduces system complexity

Page 19: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Inheritance & Generalization / Specialization Hierarchies

• Generalization / Specialization Hierarchies allow inheritance.

• We organize and define things and communicate about things in terms of Generalization / Specialization Hierarchies.

• Inherit means get something from.• One class of objects can inherit attributes and

methods from another.• Inheritance encourages and allows reuse.• Reuse means to use something over again, rather

than having to reinvent the wheel. • Allows compact & less redundant models

Page 20: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Inheritancehttp://www.javaworld.com/javaworld/jw-07-2001/jw-0706-java101.html

Page 21: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

Review Questions

1. Differentiate between a class and an object.2. What are attributes of a class?3. What are object relationship?4. Why is a whole-part relationship is special

type of association relationship?5. What is a method, or operation, of a class?6. What are the standard methods that any

class knows how to provide?7. What is a custom method?

Page 22: Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)

8. What are messages, and who or what is information hiding?

9. What are messages, and who or what sends them?

10.What is polymorphism?

11.Which type of hierarchy allows inheritance?

12.What two capabilities can a class inherit from another class.

13.How is inheritance related to potential reuse?

Review Questions