Upload
honorato-matthews
View
22
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Chapter 1. Introduction to Object-oriented Programming and Software Development. Chapter 1 Objectives. After you have read and studied this chapter, you should be able to Name the basic components of object-oriented programming. Differentiate classes and objects. - PowerPoint PPT Presentation
Citation preview
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 1
Chapter 1
Introduction to Object-oriented Programming
and
Software Development
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 2
Chapter 1 Objectives
After you have read and studied this chapter, you should be able to
Name the basic components of object-oriented programming.
Differentiate classes and objects.
Differentiate class and instance methods.
Differentiate class and instance data values.
Draw object diagrams using icons for classes, objects, and other components of object-oriented programming.
Describe the significance of inheritance in object-oriented programs.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 3
Classes and Objects
Object-oriented programs use objects.
An object is a thing, both tangible and intangible. Account, Vehicle, Employee, etc.
To create an object inside the computer program, we must provide a definition for objects—how they behave and what kinds of information they maintain —called a class.
An object is called an instance of a class.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 4
Graphical Representation of an Object
Account
SV129
The class name is placed inside the object icon.
The class name is placed inside the object icon.
The object’s name appears on top of the icon.
The object’s name appears on top of the icon.
An icon for an object is the rounded rectangle.
An icon for an object is the rounded rectangle.
The class name may be omitted when it is clear from the context which class the object belongs to.
The class name may be omitted when it is clear from the context which class the object belongs to.
customer1
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 5
Graphical Representation of a Class
AccountThe class name appears on top of the icon.
The class name appears on top of the icon.
An icon for a class is the rectangle.
An icon for a class is the rectangle.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 6
Instance-of Relationship
Employee
Employee
Bill
Employee
Steve
Employee
AndyThe class name can be omitted since it is clear which class these objects belong to .
The class name can be omitted since it is clear which class these objects belong to .
The dotted line shows the instance-of relationship.
The dotted line shows the instance-of relationship.
Before you can create instances of a class, the class must be defined.
Before you can create instances of a class, the class must be defined.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 7
Messages and Methods
To instruct a class or an object to perform a task, we send a message to it.
You can send a message only to the classes and objects that understand the message you sent to them.
A class or an object must possess a matching method to be able to handle the received message.
A method defined for a class is called a class method, and a method defined for an object is called an instance method.
A value we pass to an object when sending a message is called an argument of the message.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 8
Sending a Message
Message deposit with the argument 250.00 is sent to chk-008.
Message deposit with the argument 250.00 is sent to chk-008.
Account
chk-008
depositdeposit 250.00
Message name is usually omitted in the diagram.
Message name is usually omitted in the diagram.
deposit250.00
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 9
Sending a Message and Getting an Answer
This message has no argument.
This message has no argument.
Account
chk-008
getMonthlyFee
monthly fee
The method returns the value monthly fee back to the message sender.
The method returns the value monthly fee back to the message sender.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 10
Calling a Class Method
Account
getAverageBalance
average balance
The average balance of all accounts is returned.
The average balance of all accounts is returned.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 11
Summary of Class and Object Icons
<result>
<Class Name> Squared corners are used for a class and class methods.
Squared corners are used for a class and class methods.
An instance method icon is drawn in a dotted line.
An instance method icon is drawn in a dotted line.
<Class Name>
<Object Name>
<result>
Rounded corners are used for an object and instance methods.
Rounded corners are used for an object and instance methods.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 12
Class and Instance Data Values
An object is comprised of data values and methods.
An instance data value is used to maintain information specific to individual instances. For example, each Account object maintains its balance.
A class data value is used to maintain information shared by all instances or aggregate information about the instances.
For example, minimum balance is the information shared by all Account objects, whereas the average balance of all Account objects is an aggregate information.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 13
Sample Instance Data Value
Account
SV129
Account
SV506
Account
SV008
current balance current balance current balance
All three Account objects possess the same instance data value current balance.
All three Account objects possess the same instance data value current balance.
The actual dollar amounts are, of course, different.
The actual dollar amounts are, of course, different.
908.55 1304.98 354.00
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 14
Sample Class Data Value
Account
SV129
Account
SV506
Account
SV008
current balance current balance current balance908.55 1304.98 354.00
Account
minimum balance
100.00
There is one copy of minimum balance for the whole class and shared by all instances.
There is one copy of minimum balance for the whole class and shared by all instances.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 15
Variable and Constant Data Values
There are two types of data values:
Account
Account
SV129
minimum balance
100.00current balance
908.55
account prefix
6427opening balance
100.00A constant whose value must remain fixed over time.
A constant whose value must remain fixed over time.
A variable whose value can change over time.
A variable whose value can change over time.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 16
Inheritance
In object-oriented programming, we use a mechanism called inheritance to design two or more entities that are different but share many common features.
First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class inheriting everything from the common class.
We call the common class the superclass and all classes that inherit from it subclasses. We also call the superclass an ancestor and the subclass a descendant.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 17
Account and Its Subclasses
Account
minimum balance
100.00
Savings Checking
minimum balance
250.00
This class becomes the superclass of two subclasses…
This class becomes the superclass of two subclasses…
when (sub)classes inherit from it.
when (sub)classes inherit from it.
Inherited components are not shown in the subclasses unless…
Inherited components are not shown in the subclasses unless…
the subclass(es) overrides them.
the subclass(es) overrides them.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 18
Sample Inheritance HierarchyAccount
Savings Checking
SuperSaver Regular StudentInterestBearing
ATMChecking
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 19
Having Fun with Java
/*Program FunTime
The program will allow you to draw a picture by dragging a mouse (move the mouse while holding the left mouse button down; hold the button on Mac). To erase the picture and start over, click the right mouse button (command-click on Mac).
*/
import javabook.*;
class FunTime{
public static void main(String[ ] args){
SketchPad doodleBoard;
doodleBoard = new SketchPad();
doodleBoard.setVisible( true );}
}
Declare a nameDeclare a name
Create an objectCreate an object
Make it visibleMake it visible
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 20
Object Diagram for FunTime
FunTime
main
SketchPad
doodleBoard
setVisibletrue
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission
Intro to OOP with Java--Wu Chapter 1 - 21
Execution Flow of the FunTime Program
SketchPad doodleBoard;
doodleBoard = new SketchPad();
doodleBoard.setVisible( true );
SketchPad doodleBoard;
doodleBoard
doodleBoard = new SketchPad();
SketchPadSketchPad
doodleBoard.setVisible( true );