Upload
marian-rodgers
View
212
Download
0
Embed Size (px)
Citation preview
SE-1010Dr. Mark L. Hornick
1
Introduction to Object-Oriented Programming (OOP)
Part 1
SE-1010Dr. Mark L. Hornick
2
Objectives
Name the basic components of object-oriented programming.
Differentiate classes and objects.
Define class methods and attributes.
Draw UML diagrams for classes and objects.
SE-1010Dr. Mark L. Hornick
3
OOP is about classes and objects
These are two very basic concepts in OOP
…but what are they?
Java is an OOP language
SE-1010Dr. Mark L. Hornick
4
Alan Kay, who was instrumental in the creation of the first Apple Macintosh, was also the creator of the first OOP language called Smalltalk.
He defined OOP as follows:
SE-1010Dr. Mark L. Hornick
5
1. Everything is an object
An object is a thing, both tangible…
Objects often represent physical entities
SE-1010Dr. Mark L. Hornick
6
1. Everything is an object
… and intangible, like• Time
• Date
• Bank Account
• Grocery List
Or, objects can just represent ideas or concepts
SE-1010Dr. Mark L. Hornick
7
2. Every object has a type of class
Classes are more difficult to describe:• A Class is an abstraction (a blueprint,
or template) that defines the attributes and behavior of Objects that belong to the Class
• Objects assume the characteristics of a class
We say an Object is an Instance of a Class, kind of how a cake is an instance of a recipe for a cake.
We can also say that a Class Instance is an Object.
SE-1010Dr. Mark L. Hornick
8
3. An object is comprised of attributes and methods defined by its class
attributes are data that define an object’s properties• Each object has its own variables where it
can store the values of its attributes
methods are behaviors• Methods are executed when an object
receives a message to execute it. • Methods often manipulate attributes
Alan Kay’s OOP definitions, continued
SE-1010Dr. Mark L. Hornick
9
Exercise 1
List some attributes and behaviors for a BankAccount class Pretend it’s used within the software of an ATM
machine (or within a personal finance app) What does it represent? What can it do? What are its properties?
SE-1010Dr. Mark L. Hornick
10
Exercise 2
List some attributes and behaviors for a Printer class What does a printer do? What properties does it have?
SE-1010Dr. Mark L. Hornick
11
Unified Modeling Language (UML)
A notation for objects and classes. Can be applied to any OOP language (not just Java).
cd Data Model
<class name>
- <attribute1>: int- <attribute2>: int
+ <method1>() : void+ <method2>() : void
UML class diagram
SE-1010Dr. Mark L. Hornick
12
We’re not done with Alan Kay’s definitions yet…
SE-1010Dr. Mark L. Hornick
13
4. Object-oriented programs use objects
• An object-oriented program is a bunch of objects telling each other what to do by sending and receiving messages to and from one another• A message instructs an object to
execute one of it’s methods
Alan Kay’s OOP definitions, continued
SE-1010Dr. Mark L. Hornick
14
The Relationship between Messages and Methods
To instruct an object to do something, we “send a message” to it.
You can send a message only to the objects that understand the message you send to them.
In Java (and other OO languages like C++, C#, or VB), messages are sent by calling a method defined within the object’s class we also say “execute a method” or “invoke a method” and sometimes we use “function” instead of “method”
We don’t usually send messages to classes, although there are exceptions we’ll learn about later
SE-1010Dr. Mark L. Hornick
15
UML again
A UML Sequence Diagram illustrating messages being sent to an instance of a class.
hp1:Printerpurge
The message purge
The object’s name hp1 The object’s
class Printer
print(“report.txt”)
SE-1010Dr. Mark L. Hornick
16
Passing values in messages
A value we pass to an object when sending a message is called an argument of the message.
hp1:Printer
The message print with theargument “report.txt”
The object’s name hp1
The object’s class Printer
print(“report.txt”)
SE-1010Dr. Mark L. Hornick
17
Two-way communication
Many times, a method will return a message back to the sender.
hp11:Plotter
getPaperLevel()
The method
The object’s name The object’s
classNo argument
27
The return value
SE-1010Dr. Mark L. Hornick
18
Many arguments can be sent in an originating message, but only one return value can be sent back
hp11:Plotter
setFont(“Arial”, “italic”)
The method
The object’s name The object’s
classTwo arguments
“ok”
The return value
SE-1010Dr. Mark L. Hornick
19
Object-oriented programming
• The object-oriented (OO) approach provides tools for the programmer to represent elements in the problem, or domain space
• Elements in the problem space, and their representation in the solution space, are referred to as “objects”
• OO allows a programmer to define a class (the type of an object) to fit the problem, rather than being forced into existing data types representing units of storage in a machine
• Object-orientation allows you to describe the problem in terms of the problem, rather than in terms of the solution