CS 102
Object Oriented Programming
First Class: Bank Account
Bank Account
3
¨ Lets implement a bank account program
¨ What type of information do we need for a bank
account?
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
4
¨ Lets implement a bank account program
¨ What type of information do we need for a bank
account?
¤ Account ID
¤ Balance
¤ Currency
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
5
¨ Lets implement a bank account program
¨ What type of information do we need for a bank
account?
¤ Account ID (int)
¤ Balance (double)
¤ Currency (String)
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
6
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
7
¨ int and double are primitive types
¨ String is an object type
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
8
¨ int and double are primitive types
¨ String is an object type
¨ What is primitive type? What is object type?
Ozyegin University - CS 102 - Object Oriented Programming
Primitive types
9
¨ Primitive data types are predefined by the
language and named by a keyword.
¨ Java language has 8 types
¤ byte
¤ short (16 bit signed)
¤ int (32 bit signed)
¤ long (64 bit)
¤ float (32 bit floating point)
¤ double (64 bit floating point)
¤ boolean
¤ char
Ozyegin University - CS 102 - Object Oriented Programming
Object types
10
¨ Everything else that is not primitive
¤ Created by the programmer not defined by Java
¤ All other user defined classes
¨ An object can be created with the new keyword
¤ int [] myArray = new int [10];
¨ When new keyword is used, some space to store
this object is allocated from the memory.
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
11
¨ int and double are primitive types
¨ String is an object type
¨ How are they represented in memory?
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
12
¨ Primitive types are stored in Stack
¨ Objects are stored in Heap
¨ What is Stack and Heap?
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
13
¨ When you declare a variable in a program, Java
allocates space for that variable from one of
several memory regions.
¤ Heap
¤ Stack
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
14
¨ Heap
¤ Holds objects created in the program
¨ Stack
¤ Used during the execution of the program
¤ Stack holds
n short lived objects (local primitive types)
n When a function is called a block of memory (stack
frame) is allocated to hold the local variables. It is
removed when the execution of function finishes
n references to other objects in the heap
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
15
¨ Heap vs. Stack
¤ Heap holds the objects whereas Stack holds reference
to these objects
¨ Objects
¤ When new keyword is used, some space to store this
object is allocated from the heap memory.
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
16
Variable declaration
¨ Primitive type
¤ int myInt; myInt
¨ Object type
myString
¤ String myString;
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
17
Variable assignment
¨ Primitive type
¤ int myInt; myInt
¤ myInt = 5; 5
¨ Object type
myString
¤ String myString;
1234
¤ myString = new String(“Text”);
Address of String object in heap 1234 “Text”
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
18
Variable assignment
¨ Primitive type
¤ int myInt;
myString keeps reference to the String
myIntobject.
It =keeps
¤ myInt 5; the location (address) of the object.
5
¨ Object type
myString
¤ String myString;
1234
¤ myString = new String(“Text”);
Address of String object in heap 1234 “Text”
Ozyegin University - CS 102 - Object Oriented Programming
Memory Allocation
19
Variable assignment
¨ Primitive type
¤ int myInt; myInt
¤ myInt = 5;
5
¨ Object type
¤ String myString;
myString
¤ myString = new String(“Text”);
¨ Instead of showing the address,
we will use an arrow
“Text”
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
20
account1ID
1
¨ Primitive types are stored in Stack
account1Balance
1000
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
21
account1Currency
¨ Objects are stored in Heap
¨ Their reference is stored in Stack
“TL”
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
22
account1ID account2ID
1 2
account1Balance account2Balance
1000 800
account1Currency account2Currency
“TL” “US”
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account
23
Ozyegin University - CS 102 - Object Oriented Programming
Depositing Money
24
account1Balance account2Balance
¨ Before
1000 800
Ozyegin University - CS 102 - Object Oriented Programming
Depositing Money
25
account1Balance account2Balance
¨ After
1050 1100
Ozyegin University - CS 102 - Object Oriented Programming
Printing Account Details
26
Ozyegin University - CS 102 - Object Oriented Programming
Account Class
27
¨ Each account has an ID, balance and currency.
¤ These can be thought as attributes of an account.
¨ Can we have an account object which holds all
these necessary information together?
Ozyegin University - CS 102 - Object Oriented Programming
Account Class
28
¨ Each account has an ID, balance and currency.
¨ Can we have an account object which holds all these
necessary information together?
Ozyegin University - CS 102 - Object Oriented Programming
Account Class
29
¨ Each account has an ID, balance and currency.
¨ Can we have an account object which holds all these
necessary information together?
¨ These are called member variables of the class or fields.
¨ They are also referred as instance variables.
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account – version 1
30
Account.java
AccountTest.java
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account – version 1
31
Account.java
This is the class which
provides the specifics
of the Account object
AccountTest.java
In here, we have
two Account
objects: account1
and account2
Ozyegin University - CS 102 - Object Oriented Programming
Version 1 - Memory Layout
32
Any idea how this is going
to be kept in memory?
Ozyegin University - CS 102 - Object Oriented Programming
Version 1 - Memory Layout
33
account1 account2
Ozyegin University - CS 102 - Object Oriented Programming
Version 1 - Memory Layout
34
account1 account2
number number
1 2
balance balance
100 200
currency currency
Ozyegin University - CS 102 - Object Oriented Programming
Version 1 - Memory Layout
35
account1 account2
number number
1 2
balance balance
100 200
currency currency
“TL”
Ozyegin University - CS 102 - Object Oriented Programming “USD”
Printing Class Variables
36
Ozyegin University - CS 102 - Object Oriented Programming
What will be the output?
37
Ozyegin University - CS 102 - Object Oriented Programming
What will be the output?
38
Ozyegin University - CS 102 - Object Oriented Programming
Change in memory…
39
¨ Before the deposit: account1 account2
number number
1 2
balance balance
100 200
currency currency
“TL”
Ozyegin University - CS 102 - Object Oriented Programming “USD”
Change in memory…
40
¨ After the deposit: account1 account2
number number
1 2
balance balance
150 500
currency currency
“TL”
Ozyegin University - CS 102 - Object Oriented Programming “USD”
Object Functionality
41
¨ Depositing money to an account is actually a
functionality of an account.
¤ All accounts can be deposited some amounts of money
¨ How can we make depositing money a functionality
of account object?
Ozyegin University - CS 102 - Object Oriented Programming
Object Functionality
42
¨ Depositing money to an account is actually a
functionality of an account.
¤ All accounts can be deposited some amounts of money
¨ How can we make depositing money a functionality
of account object?
¤ By defining it as a member function…
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account – version 2
43
Ozyegin University - CS 102 - Object Oriented Programming
deposit member function
44
¨ Before deposit member function
¨ After deposit member function
Ozyegin University - CS 102 - Object Oriented Programming
Change in memory… (same as before)
45
¨ Before the deposit: account1 account2
number number
1 2
balance balance
100 200
currency currency
“TL”
Ozyegin University - CS 102 - Object Oriented Programming “USD”
Change in memory… (same as before)
46
¨ After the deposit: account1 account2
number number
1 2
balance balance
150 500
currency currency
“TL”
Ozyegin University - CS 102 - Object Oriented Programming “USD”
Class
47
¨ We have written our first class!
Ozyegin University - CS 102 - Object Oriented Programming
Class
48
¨ We have written our first class!
member variables or
instance variables
member
functions
Ozyegin University - CS 102 - Object Oriented Programming
From last lecture…
49
¨ Our proposed programs need to match to the
problem we are trying to solve
¤ In the problem, what are the real world objects?
n what kind of information do they hold? (attributes)
n what kind of functionalities they have? (behavior)
¤ Solve the problem in terms of these objects
n Objects in the real world ~ Objects in our programs
n Low representational gap
¤ The object oriented programming
Source: “Core: Defining Classes and
Ozyegin University - CS 102 - Object Oriented Programming Creating Objects” Lecture Notes from
UCSD Course on OOP in Java
Class
50
¨ We have written our first class!
member variables or
attributes
instance variables
behaviors member
functions
Ozyegin University - CS 102 - Object Oriented Programming
Objects
51
¨ We have also used this class to create objects!
Ozyegin University - CS 102 - Object Oriented Programming
Objects
52
¨ We have also used this class to create objects!
Ozyegin University - CS 102 - Object Oriented Programming
Objects
53
¨ We have also used this class to create objects!
Ozyegin University - CS 102 - Object Oriented Programming
Program Output
54
Ozyegin University - CS 102 - Object Oriented Programming
Report Account Information
55
Ozyegin University - CS 102 - Object Oriented Programming
Report Account Information
56
How can we fix this?
Ozyegin University - CS 102 - Object Oriented Programming
Report Account Information
57
¨ Reporting its information can be a functionality of
accounts.
¨ report member function!
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account – version 3
58
¨ report member function!
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account – version 3
59
¨ report member function!
Ozyegin University - CS 102 - Object Oriented Programming
Bank Account – version 3
60
Ozyegin University - CS 102 - Object Oriented Programming
Member Functions
61
account1 ¨ Member functions can account2
be also represented in
memory diagrams
number number
1 2
balance balance
150 500
currency currency
“TL” “USD”
deposit(double) deposit(double)
report()Ozyegin University - CS 102 - Object Oriented Programming report()
Exercise: What is the output?
62
Ozyegin University - CS 102 - Object Oriented Programming
Exercise: What is the output?
63
Ozyegin University - CS 102 - Object Oriented Programming
Exercise (Bank Account – version 4)
64
Ozyegin University - CS 102 - Object Oriented Programming
Exercise
65
Ozyegin University - CS 102 - Object Oriented Programming
Exercise
66
Ozyegin University - CS 102 - Object Oriented Programming
Exercise
67
Ozyegin University - CS 102 - Object Oriented Programming
Exercise
68
Ozyegin University - CS 102 - Object Oriented Programming
Exercise
69
Ozyegin University - CS 102 - Object Oriented Programming
Exercise: What will be the output?
70
Ozyegin University - CS 102 - Object Oriented Programming
Exercise: What will be the output?
71
Ozyegin University - CS 102 - Object Oriented Programming
Exercise
72
Ozyegin University - CS 102 - Object Oriented Programming
75 Any Questions ?
Ozyegin University - CS 102 - Object Oriented Programming