Classes and Objects

Download Report

Transcript Classes and Objects

Chapter 1
Introduction to Object-Oriented
Programming and Software
Development
Chapter 1 - 1
Objectives
•Understand the basic of object-oriented
programming
– Differentiate classes and objects.
– Differentiate class and instance methods.
– Differentiate class and instance data values.
– Draw UML diagrams for classes and objects
– Describe significance of inheritance in object-oriented
programs
•Name and explain the stages of the software
lifecycle
Chapter 1 - 2
What is an Object ?
• Real-world objects:
– Concrete objects: Apple1, Car1, TV2, Teacher2,
Student3, …
– Conceptual Objects: 1, 2.3, Date1, Meeting2, point2, …
• Objects have:
– Properties (attributes): color, weight, height, sex, name,
speed, position,…
– Capabilities (behaviors): can receive commands(,
request, query) and respond (do actions) based on its
internal states to change its internal state and/or
external environment.
• The properties of an object constitutes its current
state.
Chapter 1 - 3
What is a Software object ?
• a software bundle of data and functions used to
model real-world objects you find in everyday life.
• In OOP, Software objects are building block of software
systems
– program is a collection of interacting objects
– objects cooperate to complete a task
– to do this, they communicate by sending “messages” to each other
• Software objects can model
– tangible things: School, Car, Bicycle,
– conceptual things: meeting, date
– Processes: finding paths, sorting cards
• Note: Software objects are only abstraction of real-world
objects; properties and behavior of irrelevance will not be
modeled in software objects.
Chapter 1 - 4
What is a Java Object?
• In Java, an object consists of 0 or more fields and 0 or more
methods.
– Fields are used to model properties.
– Methods are used to model capabilities.
• Fields are variables.
– like the fields of a C struct.
– An object without methods is equivalent to a C struct.
• A method is similar to a C function.
– Normally, it will operate on the fields of the object.
– These fields are accessible by name in the method.
• Java variables can not hold objects, but only references to
them.
– Object do not have a names.
– Object are created only at runtime.
Chapter 1 - 5
Classes and Objects
• Current conception:
– a java/software object  a real-life object,
– e.g., a Java car  a real car
• Disadvantage: impractical to work with objects this way
– may be indefinitely many (i.e., modeling all atoms in the universe)
– do not want to describe each individual separately, because they
may have much in common
• Classifying objects into classes of similar
properties/behaviors
–
–
–
–
–
factors out commonality among sets of similar objects
lets us describe what is common once
then “stamp out” any number of copies later
Ex: Student: { S1, S2, S3 } Course:{C1,C2,C3} Teacher:{ T1,T2}
but not {s1, t1}, {s2, t2}, {c1,c2,c3,s3}
• Analog:
– blueprint
– constructions
(class)
(objects)
Chapter 1 - 6
What is a Java Class?
• In Java, a class is a template (textual
description) of a set of similar objects.
– All objects in the class have the same types of
properties and the same set of capabilities.
• It defines the fields and methods that all
objects in that class will have.
– Classes have names.
– Class appear in the text of your program.
– A java program consists of a set of classes.
• A defined class is a Java Type, so you can
have objects or variables of that type.
Chapter 1 - 7
Example
class Person {
// fields or properties
int age ;
String name ;
Person father, mother ;
Person[] : siblings
// methods or behavior capability
void eat( Food food) ;
void play() ;
void work() ;
void sleep(); }
//possible statements
m1 = new Person(); p1 = new Person;
p1.mother = m1;
Chapter 1 - 8
Classes and Objects
• In OOP, a running application is results of
participating objects and their interactions.
• Similar objects are described or defined by the
same (set of ) classes
– properties  fields
– behavior capability  methods
• An object is called an instance of its defined
classes.
– Ex: stusent1 is an instance of both class Student and
Person and Object(萬物).
Chapter 1 - 9
Graphical Representation of a Class
<Class Name>
Example:
Account
We use a rectangle to
represent a class with
its name appearing
inside the rectangle.
Motorcycle
The notation we used here is based on the industry standard notation
called UML, which stands for Unified Modeling Language.
Chapter 1 - 10
Graphical Representation of an Object
<Object Name>
We use a rectangle to
represent an object and
place the underlined
name of the object
inside the rectangle.
Example:
SV198
This is an object named
SV198.
Chapter 1 - 11
An Object with the Class Name
<Object Name> : <Class Name>
This notation indicates
the class which the
object is an instance.
Example:
SV198 : BankAccount
This tells an object
SV198 is an instance of
the BankAccount class.
Chapter 1 - 12
Messages and Methods
• How do objects interact ?
– message passing ; method requests/invocation/call
• To instruct a class or an object to perform a task, we
– send a message (method call) to it.
– the message must be understandable to the receiving classes
or objects. i.e.,
– the receiving class or an object must possess a matching
method to be able to handle the received message.
• Kinds of method:
– A method defined for a class is called a class method, and
– a method defined for all instances of a class is called an
instance method.
• A associated value we pass to an object when sending
a message is called an argument of the message.
– e.g., p1.eat( apple1 ); // receiver.method ( argument …).
Chapter 1 - 13
Sending a Message
Message deposit
with the argument
250.00 is sent to a
BankAccount object
SV198.
caller
Rreceiver
deposit 250.00
SV198 : BankAccount
Chapter 1 - 14
Sending a Message and Getting an Answer
Ask for the current
balance of this
particular account.
getCurrentBalance()
SV198 : BankAccount
current balance
The current balance of
SV198 is returned.
Chapter 1 - 15
Calling a Class Method
Ask for the maximum
possible speed for all
MobileRobot objects is
returned.
MobileRobot
getMaximumSpeed()
maximum speed
Chapter 1 - 16
Class and Instance Data Values
• An object is composed of data values (for its properties)
and methods.
–
–
–
–
Property has name, type and value
ex: int age = 10 ;
property name and type are static (time-invariant )
property value may change with time.
• An instance data value is a value of one of its properties of
an individual instance.
– For example, each BankAccount object maintains its balance.
• A class data value is a value of some property of the whole
class.
– It is shared by all instances of the class.
– Ex: minimum balance and average balance in Account class.
Chapter 1 - 17
Sample Instance Data Value
SV129 : BankAccount
SV098 : BankAccount
SV211 : BankAccount
current balance
908.55
current balance
1304.98
current balance
All three BankAccount
objects possess the
same current balance
property.
354.00
The actual dollar
amounts (data value)
are, of course,
different.
Chapter 1 - 18
Sample Class Data Value
BankAccount
minimum balance
100.00
There is one copy of
minimum balance for
the whole class and
shared by all instances.
This line is an
instance-of
relationship.
SV129 : BankAccount
SV098 : BankAccount
SV211 : BankAccount
current balance
908.55
current balance
1304.98
current balance
354.00
Chapter 1 - 19
Object Icon with Class Data Value
SV129 : BankAccount
minimum balance
100.00
current balance
908.55
When the class icon is
not shown, we include
the class data value in
the object icon itself.
Chapter 1 - 20
Class and object diagrams
Chapter 1 - 21
Inheritance
• Inheritance is a mechanism in OOP to design two
or more entities that are different but share many
common features.
– design/programming by difference.
– Features common to all classes are defined in the
superclass.
– The classes that inherit common features from the
superclass are called subclasses.
• We also call the superclass an ancestor and the subclass a
descendant.
Chapter 1 - 22
A Sample Inheritance
• Here are the superclass Account and its
subclasses Savings and Checking.
Account
Checking
Savings
Chapter 1 - 23
Inheritance Hierarchy
• An example of inheritance hierarchy among
different types of students.
Student
Graduate
Masters
Doctoral
Undergrad
Law
Commuting
Resident
Chapter 1 - 24
Software Engineering
• Much like building a skyscraper, we need a
disciplined approach in developing complex
software applications.
• Software engineering is the application of a
systematic and disciplined approach to the
development, testing, and maintenance of a
program.
Chapter 1 - 25
Software Life Cycle
• The sequence of stages from conception to
operation of a program is called software life
cycle.
• Five stages are
–
–
–
–
–
–
Analysis
Design
Coding
Testing
Operation and Maintenance
Update and Evolution
Chapter 1 - 26