ACM/JETT Workshop - August 4-5, 2005 1 03.Object-Oriented Basics & Design.

26
1 ACM/JETT Workshop - August 4-5, 2005 03.Object-Oriented Basics & Design

Transcript of ACM/JETT Workshop - August 4-5, 2005 1 03.Object-Oriented Basics & Design.

1 ACM/JETT Workshop - August 4-5, 2005

03.Object-Oriented Basics & Design

2 ACM/JETT Workshop - August 4-5, 2005

TopicsIn 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.

3 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.

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.

bankaccountbankaccountbankbank

customer

showBalance (acctId)

getBalance()

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.

bankaccountbankaccountbankbank

customer

showBalance (acctId)

getBalance()

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.

8 ACM/JETT Workshop - August 4-5, 2005

What are objects?Objects encapsulate data and behavior.

A Bank Account object has – acctId– balance– calcInterest ()– deposit ()– withdraw ()– getBalance()

data

methods (functions)(behavior)

9 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.

10 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.

11 ACM/JETT Workshop - August 4-5, 2005

Objects = data + methods

An object of type BankAccount

acctId

deposit()

withdraw()

private public

calcInterest()

balance

getBalance()

Client

A client can call any public methods

data

methods

12 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.

13 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.

14 ACM/JETT Workshop - August 4-5, 2005

Class vs Object

acctId

deposit()

withdraw()

private public

calcInterest()

balance

getBalance()

acctId=A12

deposit()

withdraw()

private public

calcInterest()

Balance=100.00

getBalance()

BankAccount – a classmyAccount – an object

of type BankAccount

15 ACM/JETT Workshop - August 4-5, 2005

Class vs Object• How do you define a class in Java?

Show class BankAccountExample1_3

Show class BankAccountExample1_3

16 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().

17 ACM/JETT Workshop - August 4-5, 2005

Creating objects from a class

// Declare a variable // of type BankAccount

BankAccount myAcct;myAcct

// Create an instance of type BankAccount// Store the reference in the variable// myAcct

myAcct = new BankAccount();acctId = “Not set”

Balance = 0.0

myAcct

18 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);

19 ACM/JETT Workshop - August 4-5, 2005

Instance data members of an object

// Declare a variable of type BankAccount

BankAccount myAcct;myAcct = new BankAccount();

BankAccount acct2 = new BankAccount();

// deposit 100.00 into myAcct.

myAcct.deposit(100.00);

// deposit 500.00 into acct2.

acct2.deposit(500.00);

myAcct

acctId = “Not set”balance = 0.0

acct2acctId = “Not set”

balance = 0.0

The instances myAcct and acct2 have their own copy of the data members, balance and acctId (instance data members)

20 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.

21 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();

22 ACM/JETT Workshop - August 4-5, 2005

Class-level Data members

Show class BankAccountExample2_3

Show class BankAccountExample2_3

23 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.

24 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.

25 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.

26 ACM/JETT Workshop - August 4-5, 2005

To test your understanding

• Do Lab 1