chapter 4 basic object-oriented concepts. chapter 4 objectives class vs. object attributes of a...
Post on 19-Jan-2016
231 Views
Preview:
TRANSCRIPT
Chapter 4
Basic Object-Oriented Concepts
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
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
• 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
• 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
Class, data entities, relational tables
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
Classes and Objects
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.
• 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
• 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
Whole / PartC
om
pon
en
ts
Ag
gre
gati
on
Part
Wh
ole
Su
per
Cla
ss
Su
b C
lass
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
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
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
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”
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.
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
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
Inheritancehttp://www.javaworld.com/javaworld/jw-07-2001/jw-0706-java101.html
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?
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
top related