Object-Oriented Basics & Design

Download Report

Transcript Object-Oriented Basics & Design

03.Object-Oriented Basics &
Design
1
ACM/JETT Workshop - August 4-5, 2005
Topics
In this Lecture, we will discuss the some of the
fundamental notions in Object-Oriented
systems:
– Encapsulation of data and functions as
objects.
– Class vs object
– Information hiding using public and private
– Creating instances /objects from classes.
– Calling methods on objects
– Instance vs class-level data members.
– Java code segments to illustrate these
concepts.
2
ACM/JETT Workshop - August 4-5, 2005
What to expect
• After this lecture, you should be able to
use the given code in BankAccount.java
file and do the following:
– Add a new method to class BankAccount.
– Create instances from class BankAccount.
– Use the methods defined in BankAccount
– Compile and test your code.
3
ACM/JETT Workshop - August 4-5, 2005
An Object-Oriented System
An object-oriented system consists of a
network of objects which collaborate to
solve a problem.
Objects store data and perform actions.
getBalance()
bankaccount
bank
showBalance (acctId)
customer
ACM/JETT Workshop - August 4-5, 2005
An Object-Oriented (OO) System
To understand the object-oriented concepts
and OO design, we will study a small part of
an application area that we are all familiar
with: banking.
A simple specification of banking:
A bank maintains a collection of bank
accounts, where a bank account can be a
checking or a savings account.
Each bank account is associated with a
customer. A customer queries,deposits
and/or withdraws money into a bank
account.
ACM/JETT Workshop - August 4-5, 2005
An Object-Oriented System
The figure below shows some of the objects in the
specification and a scenario where a customer
calls a showBalance() function on a bank object;
in turn, a bank object calls getBalance() function
on a bank account object.
getBalance()
bankaccount
bank
showBalance (acctId)
customer
ACM/JETT Workshop - August 4-5, 2005
What are Objects?
Objects are “ things” that encapsulate data
and behavior (actions called methods).
Objects can be “physical” things (eg:
teacher, book, fish).
Objects can be “conceptual” things (eg:
registration, payment, bank account).
Objects correspond to “nouns” in problem
description.
ACM/JETT Workshop - August 4-5, 2005
What are objects?
Objects encapsulate data and behavior.
A Bank Account object has
– acctId
data
– balance
– calcInterest ()
– deposit ()
methods (functions)
(behavior)
– withdraw ()
– getBalance()
8
ACM/JETT Workshop - August 4-5, 2005
What are objects?
• Objects may have an internal structure
(private).
– Eg: A bank account may store an acctId,
balance etc.
• Objects reveal themselves by their
external behavior (public methods).
– Eg: A back account object may be used
with its public methods, like deposit and
withdraw.
9
ACM/JETT Workshop - August 4-5, 2005
What are objects?
• Objects reveal their “public behavior” but
keep their "inside (or implementation)"
hidden.
• The “public behavior” of objects is through
a set of methods (functions) that are
offered to clients of objects.
• The methods (functions) can be invoked
by clients.
• A client is an object that invokes the
methods of an object.
10
ACM/JETT Workshop - August 4-5, 2005
Objects = data + methods
An object of type BankAccount
private
Client
public
deposit()
acctId
data
A client can call any
public methods
withdraw()
balance
getBalance()
calcInterest()
methods
11
ACM/JETT Workshop - August 4-5, 2005
What is a class
• A class is a software blueprint that
defines the variables and the methods
common to all objects of a certain kind.
• A class can be used and reused many
times to create many objects.
• A class is one mechanism to define new
types in Java.
• Objects are created as instances of a
class.
12
ACM/JETT Workshop - August 4-5, 2005
Information hiding
• In a class,
• public features can be seen and
manipulated by anybody -- they are the
external (interface) view.
• private features can be manipulated by
only the other members of that class.
They are the internal (implementation)
view.
• Good OO design recommends that
– methods are public and
– data fields are private.
13
ACM/JETT Workshop - August 4-5, 2005
Class vs Object
private
private
public
deposit(
deposit()
acctId=A12
acctId
withdraw
withdraw()
Balance=100.00
balance
getBalanc
getBalance()
calcInterest()
calcInterest()
BankAccount – a class
14
public
myAccount – an object
of type BankAccount
ACM/JETT Workshop - August 4-5, 2005
Class vs Object
• How do you define a class in Java?
Show class BankAccount
Example1_3
15
ACM/JETT Workshop - August 4-5, 2005
Creating objects from a class
• Objects are created by instantiating a
class.
• An object is also called an instance.
• In Java, you create an object from a class
using new().
16
ACM/JETT Workshop - August 4-5, 2005
Creating objects from a class
// Declare a variable
// of type BankAccount
myAcct
BankAccount myAcct;
// Create an instance of type BankAccount
// Store the reference in the variable
// myAcct
acctId = “Not set”
Balance = 0.0
myAcct = new BankAccount();
myAcct
17
ACM/JETT Workshop - August 4-5, 2005
Calling Methods of an objects
The public methods of an object can be
invoked with a . (dot) operator.
Example:
BankAccount myAcct;
myAcct = new BankAccount();
// deposit $100.00 by invoking the
// deposit() method on myAcct
myAcct.deposit(100.00);
18
ACM/JETT Workshop - August 4-5, 2005
Instance data members of an object
// Declare a variable of type BankAccount
BankAccount myAcct;
myAcct = new BankAccount();
acctId = “Not set”
balance = 0.0
myAcct
acct2
acctId = “Not set”
balance = 0.0
BankAccount acct2 = new BankAccount();
// deposit 100.00 into myAcct.
The instances myAcct and
myAcct.deposit(100.00); acct2 have their own copy
// deposit 500.00 into acct2.
acct2.deposit(500.00);
19
of the data members,
balance and acctId
(instance data members)
ACM/JETT Workshop - August 4-5, 2005
Class-level Data members
• Assume that we want to include the fact
that the current interest rate for all bank
accounts is 5%.
• If the rate changes, all bank account
instances should have the same rate.
• In this case, we can define the interest
rate as a class level data member.
• In Java, class-level members are called
static members.
• Both data and methods can be static.
20
ACM/JETT Workshop - August 4-5, 2005
Class-level Data members
• Methods that are static can only access
static data members.
• Static methods can be called on a class
without creating an instance.
• Example:
BankAccount myAcct = new BankAccount();
// Calling an instance method on an instance
myAcct.deposit (100.00);
// Calling a static method on a class
double rate = BankAccount.getRate();
21
ACM/JETT Workshop - August 4-5, 2005
Class-level Data members
Show class BankAccount
Example2_3
22
ACM/JETT Workshop - August 4-5, 2005
State of an object
• The state of an object is described by
the values of (some or all) of its data
members.
• Example: The state of a bank account
object would include the current balance.
23
ACM/JETT Workshop - August 4-5, 2005
When do objects change their state?
• Objects change their state as a result
of executing a method.
• Example:
• The state of a BankAccount object
(with a current balance of $0) will
change when a deposit() or withdraw()
method is invoked on that object.
24
ACM/JETT Workshop - August 4-5, 2005
The topics that you should be
comfortable with at this time
Class vs object
How to define a class in Java
private vs public members of a class
Creating instances/objects from classes in
Java.
Calling instance methods on objects and
Calling class methods on class/object using
the Java code segments given.
25
ACM/JETT Workshop - August 4-5, 2005
To test your understanding
• Do Lab 1
26
ACM/JETT Workshop - August 4-5, 2005