Object-Oriented Programming Overview

C
S
1
0
5
I
n
t
r
o
d
u
c
t
i
o
n
 
t
o
 
O
b
j
e
c
t
-
O
r
i
e
n
t
e
d
P
r
o
g
r
a
m
m
i
n
g
P
r
o
f
.
 
D
r
.
 
N
i
z
a
m
e
t
t
i
n
 
A
Y
D
I
N
n
a
y
d
i
n
@
i
t
u
.
e
d
u
.
t
r
n
i
z
a
m
e
t
t
i
n
.
a
y
d
i
n
@
o
z
y
e
g
i
n
.
e
d
u
.
t
r
 
1
 
E
x
t
e
n
d
i
n
g
 
B
a
n
k
 
A
c
c
o
u
n
t
E
x
a
m
p
l
e
2
Outline
Primitive types
Object types
Memory Allocation
Heaps
Member Functions
Memory Model
Class Instances
Standard Streams
Arrays
3
B
a
n
k
 
A
c
c
o
u
n
t
Lets implement a bank account program
What type of information do we need for a bank account?
Account ID (int)
Balance (double)
Currency (String)
4
B
a
n
k
 
A
c
c
o
u
n
t
 
 
 
 
 
 
 
 
 
 
i
nt 
and 
double
 are primitive types
S
t
r
i
n
g
 
i
s
 
a
n
 
o
b
j
e
c
t
 
t
y
p
e
What is primitive type? What is object type?
5
P
r
i
m
i
t
i
v
e
 
t
y
p
e
s
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
c
har
6
O
b
j
e
c
t
 
t
y
p
e
s
Everything else that is not primitive
Arrays
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.
Where in memory?
7
B
a
n
k
 
A
c
c
o
u
n
t
 
 
 
 
 
 
 
 
 
 
int
 and 
double
 are primitive types
S
t
r
i
n
g
 
i
s
 
a
n
 
o
b
j
e
c
t
 
t
y
p
e
How are they represented in memory?
8
B
a
n
k
 
A
c
c
o
u
n
t
 
 
 
 
 
 
 
 
 
Primitive types are stored in 
Stack
Objects are stored in 
Heap
What is 
Stack
 and 
Heap
?
9
M
e
m
o
r
y
 
A
l
l
o
c
a
t
i
o
n
 
When you declare a variable in a program, Java allocates
space for that variable from one of several memory
regions
:
H
e
a
p
Holds objects created in the program
S
t
a
c
k
Used during the execution of the program
Stack holds
short lived objects (local primitive types)
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
references to other objects in the heap
 
 
10
M
e
m
o
r
y
 
A
l
l
o
c
a
t
i
o
n
 
Heap vs. Stack
Heap holds the objects where Stack holds reference to these
objects
Objects
When new keyword is used, some space
 to
 store this object is
allocated from the heap memory.
V
a
r
i
a
b
l
e
 
d
e
c
l
a
r
a
t
i
o
n
:
Primitive type
 
int myInt;
 
Object type
 
String myString;
 
11
H
e
a
p
s
A 
heap
 (or, for greater clarity, max-heap) is a binary tree
that:
is almost complete: all nodes are filled except the last level may
have some missing toward the right.
all nodes store values that are at least as large as the values
stored in their descendants.
The heap property ensures that the tree’s largest element
is stored in the root
The shape of a heap is 
very regular
In a heap, the left and 
right subtrees both store 
elements that are 
smaller
 
than the root 
element
12
M
e
m
o
r
y
 
A
l
l
o
c
a
t
i
o
n
 
V
a
r
i
a
b
l
e
 
a
s
s
i
g
n
m
e
n
t
:
 
Primitive type
 
int myInt;
 
myInt = 5;
 
 
Object type
 
String myString;
 myString = new String("Text");
 
13
M
e
m
o
r
y
 
A
l
l
o
c
a
t
i
o
n
 
V
a
r
i
a
b
l
e
 
a
s
s
i
g
n
m
e
n
t
:
 
Primitive type
 
int myInt;
 
myInt = 5;
 
 
Object type
 
String myString;
 myString = new String("Text");
 
Instead of showing the address,
we will use an arrow
14
B
a
n
k
 
A
c
c
o
u
n
t
 
 
 
 
 
 
 
 
 
Primitive types are stored in 
Stack
15
B
a
n
k
 
A
c
c
o
u
n
t
Objects are stored in 
Heap
Their reference is stored in 
Stack
16
B
a
n
k
 
A
c
c
o
u
n
t
17
M
e
m
b
e
r
 
F
u
n
c
t
i
o
n
s
Member functions can also be represented in memory diagrams
18
C
u
r
r
e
n
t
 
A
c
c
o
u
n
t
 
C
l
a
s
s
 
(
V
e
r
s
i
o
n
 
1
5
)
 
19
A
n
o
t
h
e
r
 
c
l
a
s
s
 
Lets add another class
Customer object
Name
Account
20
C
u
s
t
o
m
e
r
 
C
l
a
s
s
 
21
U
s
i
n
g
 
C
u
s
t
o
m
e
r
 
C
l
a
s
s
 
 
 
 
 
 
 
 
 
 
 
 
Draw the Memory Model
22
D
r
a
w
 
t
h
e
 
M
e
m
o
r
y
 
M
o
d
e
l
 
23
W
h
a
t
 
i
s
 
t
h
e
 
o
u
t
p
u
t
?
 
24
B
e
f
o
r
e
 
a
n
d
 
A
f
t
e
r
 
d
e
p
o
s
i
t
 
25
W
h
a
t
 
i
s
 
t
h
e
 
o
u
t
p
u
t
?
26
B
e
f
o
r
e
 
a
n
d
 
A
f
t
e
r
 
s
e
t
C
u
r
r
e
n
c
y
 
27
W
h
a
t
 
i
s
 
t
h
e
 
f
i
n
a
l
 
m
e
m
o
r
y
 
m
o
d
e
l
?
 
28
 
 
29
 
 
30
 
 
31
A
d
d
i
t
i
o
n
a
l
 
C
l
a
s
s
e
s
 
We have customer and account, lets have a bank then.
A bank has a name and customers.
 
B
a
n
k
 
C
l
a
s
s
 
 
C
l
a
s
s
 
I
n
s
t
a
n
c
e
s
:
Only one name but multiple customers.
name (String)
customers (array)
 
 
 
How many customers?
Need to know in advance, why?
 
32
B
a
n
k
 
C
l
a
s
s
 
 
C
l
a
s
s
 
I
n
s
t
a
n
c
e
s
 
Lets say a bank can have at most 3 customers.
Create an array of size 3
 
But you don’t have to use all 3 customers.
It can be less.
Therefore keep the number of customers value in a variable.
33
B
a
n
k
 
C
l
a
s
s
 
-
 
C
o
n
s
t
r
u
c
t
o
r
Initially banks have no customers.
What should be the constructor arguments?
34
B
a
n
k
 
C
l
a
s
s
 
 
A
d
d
i
n
g
 
C
u
s
t
o
m
e
r
s
An addCustomer method to add customers.
This method takes one customer as an argument.
It updates the array and the numCustomers value.
35
B
a
n
k
 
C
l
a
s
s
 
 
O
t
h
e
r
 
F
u
n
c
t
i
o
n
s
36
B
a
n
k
 
A
p
p
l
i
c
a
t
i
o
n
Assume that we have an application which takes customer
information in runtime from users.
We need to use Scanner in order to read the input from the
console.
37
S
t
a
n
d
a
r
d
 
S
t
r
e
a
m
s
 
38
B
a
n
k
 
A
p
p
l
i
c
a
t
i
o
n
 
For each customer, what kind of information do we need?
 
Name
Account
Balance
Currency
Number?
The system can assign the next available account number to the account.
Need to keep a counter for account number.
39
B
a
n
k
 
A
p
p
l
i
c
a
t
i
o
n
 
40
M
e
m
o
r
y
 
M
o
d
e
l
?
 
What will be the memory model after user enters
‘‘Ali’’ for customer name
‘‘TL’’ for account’s currency
100 for initial balance
 
 
How many objects are we going to create?
41
M
e
m
o
r
y
 
M
o
d
e
l
The path is from inside out.
User entered
"
T
L
"
 
f
o
r
 
a
c
c
o
u
n
t
s
 
c
u
r
r
e
n
c
y
 
100 for initial balance
 
Return its reference to
 Customer constructor.
42
M
e
m
o
r
y
 
M
o
d
e
l
Save customer’s 
address at bank’s 
customer array
43
M
e
m
o
r
y
 
M
o
d
e
l
 
44
M
e
m
o
r
y
 
M
o
d
e
l
 
45
A
r
r
a
y
s
 
Arrays are fixed length.
We need a data structure that can be resized.
ArrayList
 
 
ArrayList
Dynamic in size
See ArrayList slides ...
46
B
a
n
k
 
C
l
a
s
s
with ArrayList
We don’t need 
numCustomers 
anymore.
47
 
A
n
y
 
Q
u
e
s
t
i
o
n
s
?
48
Slide Note

Copyright 2000 N. AYDIN. All rights reserved.

Embed
Share

This content dives into the fundamentals of object-oriented programming through images and text, covering topics such as primitive and object types, memory allocation, bank account implementation, and more. It explores the concept of stack and heap memory, as well as the distinction between primitive types stored in the stack and objects stored in the heap.

  • Object-Oriented Programming
  • Memory Allocation
  • Bank Account
  • Primitive Types
  • Stack and Heap

Uploaded on Feb 25, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS105 Introduction to Object-Oriented Programming Prof. Dr. Nizamettin AYDIN naydin@itu.edu.tr nizamettin.aydin@ozyegin.edu.tr 1

  2. Extending Bank Account Example 2

  3. Outline Primitive types Object types Memory Allocation Heaps Member Functions Memory Model Class Instances Standard Streams Arrays 3

  4. Bank Account Lets implement a bank account program What type of information do we need for a bank account? Account ID (int) Balance (double) Currency (String) 4

  5. Bank Account int and double are primitive types String is an object type What is primitive type? What is object type? 5

  6. Primitive types 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 6

  7. Object types Everything else that is not primitive Arrays 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. Where in memory? 7

  8. Bank Account int and double are primitive types String is an object type How are they represented in memory? 8

  9. Bank Account Primitive types are stored in Stack Objects are stored in Heap What is Stack and Heap? 9

  10. Memory Allocation When you declare a variable in a program, Java allocates space for that variable from one of several memory regions: Heap Holds objects created in the program Stack Used during the execution of the program Stack holds short lived objects (local primitive types) 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 references to other objects in the heap 10

  11. Memory Allocation Heap vs. Stack Heap holds the objects where Stack holds reference to these objects Objects When new keyword is used, some space to store this object is allocated from the heap memory. Variable declaration: Primitive type int myInt; Object type String myString; 11

  12. Heaps A heap (or, for greater clarity, max-heap) is a binary tree that: is almost complete: all nodes are filled except the last level may have some missing toward the right. all nodes store values that are at least as large as the values stored in their descendants. The heap property ensures that the tree s largest element is stored in the root The shape of a heap is very regular In a heap, the left and right subtrees both store elements that are smaller than the root element 12

  13. Memory Allocation Variable assignment: Primitive type int myInt; myInt = 5; Object type String myString; myString = new String("Text"); 13

  14. Memory Allocation Variable assignment: Primitive type int myInt; myInt = 5; Object type String myString; myString = new String("Text"); Instead of showing the address, we will use an arrow 14

  15. Bank Account Primitive types are stored in Stack 15

  16. Bank Account Objects are stored in Heap Their reference is stored in Stack 16

  17. Bank Account 17

  18. Member Functions Member functions can also be represented in memory diagrams 18

  19. Current Account Class (Version 15) 19

  20. Another class Lets add another class Customer object Name Account 20

  21. Customer Class 21

  22. Using Customer Class Draw the Memory Model 22

  23. Draw the Memory Model 23

  24. What is the output? 24

  25. Before and After deposit 25

  26. What is the output? 26

  27. Before and After setCurrency 27

  28. What is the final memory model? 28

  29. 29

  30. 30

  31. 31

  32. Additional Classes We have customer and account, lets have a bank then. A bank has a name and customers. Bank Class Class Instances: Only one name but multiple customers. name (String) customers (array) How many customers? Need to know in advance, why? 32

  33. Bank Class Class Instances Lets say a bank can have at most 3 customers. Create an array of size 3 But you don t have to use all 3 customers. It can be less. Therefore keep the number of customers value in a variable. 33

  34. Bank Class - Constructor Initially banks have no customers. What should be the constructor arguments? 34

  35. Bank Class Adding Customers An addCustomer method to add customers. This method takes one customer as an argument. It updates the array and the numCustomers value. 35

  36. Bank Class Other Functions 36

  37. Bank Application Assume that we have an application which takes customer information in runtime from users. We need to use Scanner in order to read the input from the console. 37

  38. Standard Streams 38

  39. Bank Application For each customer, what kind of information do we need? Name Account Balance Currency Number? The system can assign the next available account number to the account. Need to keep a counter for account number. 39

  40. Bank Application 40

  41. Memory Model? What will be the memory model after user enters Ali for customer name TL for account s currency 100 for initial balance How many objects are we going to create? 41

  42. Memory Model The path is from inside out. User entered "TL" for account s currency 100 for initial balance Return its reference to Customer constructor. 42

  43. Memory Model Save customer s address at bank s customer array 43

  44. Memory Model 44

  45. Memory Model 45

  46. Arrays Arrays are fixed length. We need a data structure that can be resized. ArrayList ArrayList Dynamic in size See ArrayList slides ... 46

  47. Bank Class with ArrayList We don t need numCustomers anymore. 47

  48. Any Questions? 48

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#