Object-Oriented Programming Concepts

 
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
 
 
 
 
t
h
i
s
 
2
 
Outline
 
this
 keyword
this
 (first use case)
this
 (second use case)
Using 
this
 for constructor call
 
3
 
t
h
i
s
 
An important keyword used inside methods in order to
refer to the current object.
t
h
i
s
 
i
s
 
t
h
e
 
c
a
l
l
i
n
g
 
o
b
j
e
c
t
 
this
 is the object which called the method
The object that occurs before the dot
 
 
 
In sender:
 receiver.method(arguments)
If sender and receiver are the same:
 method(arguments) [implicit version]
 this.method(arguments) [explicit version]
 
4
 
B
a
n
k
 
A
c
c
o
u
n
t
 
Public class 
Account {
private int
 number
;
private double
 balance
;
private 
String
 currency
;
private double
 interestRate
;
public 
Account(
int 
n
,
 double 
b
,
 
String
 
c
){
number
 
=
 
n
;
if 
(
b
 
> 0)
balance
 
=
 
b
;
else
balance
 
= 0;
 
interestRate
 
= 0;
checksetCurrency(
c
);
}
public 
Account(
int 
n
,
 
String
 
c
){
number
 
=
 
n
;
balance
 
= 0;
interestRate
 
= 0;
 
checksetCurrency(
c
);
}
public 
Account(
int 
n
){
number
 
=
 
n
;
balance
 
= 0;
currency
 = 
"TL"
;
interestRate
 
= 0;
}
 
5
 
B
a
n
k
 
A
c
c
o
u
n
t
 
public int
 
getNumber() {
return
 
number
;
}
public
 double
 
getBalance() {
return
 balance
;
}
public
 
String
 
getCurrency() {
return
 
currency
;
}
public 
boolean
 
getInterestRate
() {
return
 
interestRate
;
}
public void
 
setInterestRate
(
double
 
i
) {
interestRate
 
=
 
i
;
}
public void 
setCurrency(String 
c
) {
if
 (
currency
.equals(
"TL"
) && 
c
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
c
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
c
.equals
(
"USD"
)) {
currency
 = 
c
;
}
}
 
6
t
h
i
s
 
T
h
e
r
e
 
a
r
e
 
s
e
v
e
r
a
l
 
u
s
e
 
c
a
s
e
s
 
o
f
 
t
h
i
s
1.
It is used to access the instance variables without any
confusion with parameter names.
 
public void
 
setInterestRate
(
double
 
i
) {
interestRate 
=
 
i
;
}
public void 
setCurrency(String 
c
) {
if
 (
currency
.equals(
"TL"
) && 
c
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
c
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
c
.equals
(
"USD"
)) {
currency
 = 
c
;
}
7
 
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
public void
 
setInterestRate
(
double
 
i
) {
interestRate 
=
 
i
;
}
public void 
setCurrency(String 
c
) {
if
 (
currency
.equals(
"TL"
) && 
c
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
c
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
c
.equals
(
"USD"
)) {
currency
 = 
c
;
}
One letter parameters are not very descriptive.
It is usually the convention in Java to use the instance variable
name as parameters in the corresponding set methods.
 
8
 
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
It should be smt like:
public void
 
setInterestRate
(
double
 
interestRate
) {
interestRate
 
=
 
interestRate
;
}
public void 
setCurrency(String 
currency
) {
if
 (
currency
.equals(
"TL"
) && 
currency
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
currency
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
currency
.equals
(
"USD"
)) {
currency
 = 
currency
;
}
 
parameter names are same with class instance names
no compile error
 
9
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
public void
 
setInterestRate
(
double
 
interestRate
) {
interestRate
 
=
 
interestRate
;
}
public void 
setCurrency(String 
currency
) {
if
 (
currency
.equals(
"TL"
) && 
currency
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
currency
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
currency
.equals
(
"USD"
)) {
currency
 = 
currency
;
}
 
public static void 
main(String[] 
args
) {
 
Account
 
account1
 
= 
new
 Account(1, 100, 
"TL"
);
 
account1
.
setCurrency(
"USD"
);
System.
out
.println(
account1
.getBalance()
);
System.
out
.println(
account1
.getCurrency()
);
 
account1
.
setInterestRate(
0.02
);
System.
out
.println(
account1
.getInterestRate()
);
}
 
What should be the output?
10
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
public void
 
setInterestRate
(
double
 
interestRate
) {
interestRate
 
=
 
interestRate
;
}
public void 
setCurrency(String 
currency
) {
if
 (
currency
.equals(
"TL"
) && 
currency
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
currency
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
currency
.equals
(
"USD"
)) {
currency
 = 
currency
;
}
When class instance and parameter have the same
names, the assignment operation has no effect
11
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
The parameters 
interestRate
 and 
currency
 shadow the
class instances.
 
public void
 
setInterestRate
(
double
 
interestRate
) {
interestRate
 
=
 
interestRate
;
The 
interestRate
 assigns the parameter 
interestRate
 to
itself, causing no change whatsoever.
 
I
n
 
s
u
c
h
 
c
a
s
e
s
 
y
o
u
 
c
a
n
 
u
s
e
 
t
h
e
 
t
h
i
s
 
k
e
y
w
o
r
d
 
t
o
 
r
e
a
c
h
 
t
h
e
c
l
a
s
s
 
i
n
s
t
a
n
c
e
s
.
public void
 
setInterestRate
(
double
 
interestRate
) {
this
.
interestRate
 
=
 
interestRate
;
12
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
public void
 
setInterestRate
(
double
 
interestRate
) {
interestRate
 
=
 
interestRate
;
}
public void 
setCurrency(String 
currency
) {
if
 (
currency
.equals(
"TL"
) && 
currency
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
currency
.equals(
"USD"
) && 
currency
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
currency
.equals
(
"USD"
)) {
currency
 = 
currency
;
}
public void
 
setInterestRate
(
double
 
interestRate
) {
this
.
interestRate
 
=
 
interestRate
;
}
public void 
setCurrency(String 
currency
) {
if
 (
this
.
currency
.equals(
"TL"
) && 
currency
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
this
.
currency
.equals(
"USD"
) && 
currency
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
currency
.equals
(
"USD"
)) {
this
.
currency
 = 
currency
;
}
13
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
public void
 
setInterestRate
(
double
 
interestRate
) {
this
.
interestRate
 
=
 
interestRate
;
}
public void 
setCurrency(String 
currency
) {
if
 (
this
.
currency
.equals(
"TL"
) && 
currency
.equals
(
"USD"
)) {
balance 
=
 balance 
/ 32.88;
}
if
 (
this
.
currency
.equals(
"USD"
) && 
currency
.equals
 (
"TL"
)){
balance 
=
 balance 
* 32.88;
}
if
 (
currency
.equals(
"TL"
) || 
currency
.equals
(
"USD"
)) {
this
.
currency
 = 
currency
;
}
 
public static void 
main(String[] 
args
) {
 
Account
 
account1
 
= 
new
 Account(1, 100, 
"TL"
);
 
account1
.
setCurrency(
"USD"
);
System.
out
.println(
account1
.getBalance()
);
System.
out
.println(
account1
.getCurrency()
);
 
account1
.
setInterestRate(
0.02
);
System.
out
.println(
account1
.getInterestRate()
);
}
 
What should be the output?
14
 
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
It can be also used
inside constructors
to seperate class
instances from
parameters.
 
15
 
// Constructors
public 
Account(
int 
n
,
 double 
b
,
 
String
 
c
){
number
 
=
 
n
;
if 
(
b
 
> 0)
balance
 
=
 
b
;
else
balance
 
= 0;
 
interestRate
 
= 0;
checksetCurrency(
c
);
}
public 
Account(
int 
n
,
 
String
 
c
){
number
 
=
 
n
;
balance
 
= 0;
interestRate
 
= 0;
 
checksetCurrency(
c
);
}
public 
Account(
int 
n
){
number
 
=
 
n
;
balance
 
= 0;
currency
 = 
"TL"
;
interestRate
 
= 0;
}
 
F
i
r
s
t
 
C
o
n
s
t
r
u
c
t
o
r
 
public 
Account(
int 
n
,
 double 
b
,
 
String
 
c
){
number
 
=
 
n
;
if 
(
b
 
> 0)
balance
 
=
 
b
;
else
balance
 
= 0;
 
interestRate
 
= 0;
checksetCurrency(
c
);
}
 
public 
Account(
int 
number
,
 double 
balance
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
if 
(
balance
 
> 0)
this
.
balance
 
=
 
balance
;
else
this
.
balance
 
= 0;
 
this
.
interestRate
 
= 0;
this
.
checksetCurrency(
currency
);
}
 
16
 
S
e
c
o
n
d
 
C
o
n
s
t
r
u
c
t
o
r
 
public 
Account(
int 
n
,
 
String
 
c
){
number
 
=
 
n
;
balance
 
= 0;
interestRate
 
= 0;
 
checksetCurrency(
c
);
}
 
 
 
public 
Account(
int 
number
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
this
.
balance
 
= 0;
this
.
interestRate
 
= 0;
 
checksetCurrency(
currency
);
}
 
17
 
T
h
i
r
d
 
C
o
n
s
t
r
u
c
t
o
r
 
public 
Account(
int 
n
){
number
 
=
 
n
;
balance
 
= 0;
currency
 = 
"TL"
;
interestRate
 
= 0;
}
 
 
 
public 
Account(
int 
number
){
this
.
number
 
=
 
number
;
this
.
balance
 
= 0;
this
.
currency
 = 
"TL"
;
this
.
interestRate
 
= 0;
}
 
18
 
t
h
i
s
 
(
f
i
r
s
t
 
u
s
e
 
c
a
s
e
)
 
T
o
 
b
e
 
c
o
n
s
i
s
t
e
n
t
,
 
t
h
e
 
s
a
m
e
 
c
a
n
 
b
e
 
d
o
n
e
 
i
n
 
g
e
t
 
m
e
t
h
o
d
s
.
 
public int
 
getNumber() {
return
 
this
.
number
;
}
public double
 
getBalance() {
return
 
this
.
balance
;
}
public 
String
 
getCurrency() {
return
 
this
.
currency
;
}
public boolean
 
getInterestRate() {
return
 
this
.
interestRate
;
}
 
19
 
A
 
c
o
n
c
e
p
t
u
a
l
 
m
o
d
e
l
 
f
o
r
 
t
h
i
s
 
A
 
c
o
n
c
e
p
t
u
a
l
 
m
o
d
e
l
f
o
r
 
u
n
d
e
r
s
t
a
n
d
i
n
g
 
t
h
i
s
 
E
a
c
h
 
o
b
j
e
c
t
 
i
n
 
J
a
v
a
 
h
a
s
a
n
 
i
m
p
l
i
c
i
t
 
i
n
s
t
a
n
c
e
v
a
r
i
a
b
l
e
,
 
n
a
m
e
d
 
t
h
i
s
,
t
h
a
t
 
p
o
i
n
t
s
 
t
o
 
t
h
e
 
o
b
j
e
c
t
i
t
s
e
l
f
.
 
20
 
A
 
c
o
n
c
e
p
t
u
a
l
 
m
o
d
e
l
 
f
o
r
 
t
h
i
s
 
This is not what really
happens during execution.
In Java, each object does
NOT keep a reference to
itself –
that would be a waste of
memory space.
However, take this as
simply a conceptual
model  that helps us
understand what
this means.
 
21
T
h
i
s
 
(
s
e
c
o
n
d
 
u
s
e
 
c
a
s
e
)
 
There are several use cases of this
1.
It is used to access the instance variables without any
confusion with parameter names.
 
2.
It can be used to invoke constructors of the same class.
 
W
h
e
n
 
y
o
u
 
h
a
v
e
 
m
u
l
t
i
p
l
e
 
c
o
n
s
t
r
u
c
t
o
r
s
,
 
t
h
i
s
 
k
e
y
w
o
r
d
 
c
a
n
 
b
e
u
s
e
d
 
t
o
 
c
a
l
l
 
o
t
h
e
r
 
c
o
n
s
t
r
u
c
t
o
r
s
 
w
i
t
h
 
a
 
d
i
f
f
e
r
e
n
t
 
s
e
t
 
o
f
a
r
g
u
m
e
n
t
s
.
22
 
T
h
r
e
e
 
C
o
n
s
t
r
u
c
t
o
r
s
 
private int
 number
;
private double
 balance
;
private 
String
 currency
;
 
// Constructors
public 
Account(
int 
number
,
 double 
balance
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
if 
(
balance
 
> 0)
this
.
balance
 
=
 
balance
;
else
this
.
balance
 
= 0;
 
this
.
checksetCurrency(
currency
);
}
public 
Account(
int 
number
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
this
.
balance
 
= 0;
 
checksetCurrency(
currency
);
}
public 
Account(
int 
number
){
this
.
number
 
=
 
number
;
this
.
balance
 
= 0;
this
.
currency
 = 
"TL"
;
}
 
23
 
t
h
i
s
 
(
s
e
c
o
n
d
 
u
s
e
 
c
a
s
e
)
 
private int
 number
;
private double
 balance
;
private 
String
 currency
;
 
// Constructors
public 
Account(
int 
number
,
 double 
balance
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
if 
(
balance
 
> 0)
this
.
balance
 
=
 
balance
;
else
this
.
balance
 
= 0;
 
this
.checksetCurrency(
currency
);
}
public 
Account(
int 
number
,
 
String
 
currency
){
this
(
number
, 0,
 currency
);
}
public 
Account(
int 
number
){
this
(
number
, 0,
 
"TL"
);
}
 
24
t
h
i
s
 
(
s
e
c
o
n
d
 
u
s
e
 
c
a
s
e
)
private int
 number
;
private double
 balance
;
private 
String
 currency
;
// Constructors
public 
Account(
int 
number
,
 double 
balance
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
if 
(
balance
 
> 0)
this
.
balance
 
=
 
balance
;
else
this
.
balance
 
= 0;
this
.checksetCurrency(
currency
);
}
public 
Account(
int 
number
,
 
String
 
currency
){
this
(
number
, 0,
 currency
);
}
public 
Account(
int 
number
){
this
(
number
, 0,
 
"TL"
); 
}
25
U
s
i
n
g
 
t
h
i
s
 
f
o
r
 
c
o
n
s
t
r
u
c
t
o
r
 
c
a
l
l
 
The constructor call should be the first statement in your
constructor.
 
public 
Account(
int 
number
,
 
String
 
currency
){
int 
a
;
this
(
number
, 0,
 currency
);
 
 
Otherwise, you will receive the following error!
26
t
h
i
s
public 
Account(
int 
number
,
 double 
balance
,
 
String
 
currency
){
this
.
number
 
=
 
number
;
if 
(
balance
 
> 0)
this
.
balance
 
=
 
balance
;
else
this
.
balance
 
= 0;
this
.checksetCurrency(
currency
);
}
public 
Account(
int 
number
,
 
String
 
currency
){
this
(
number
, 0,
 currency
);
}
public 
Account(
int 
number
){
Account(
number
, 0.0,
 
"TL"
); 
}
You cannot use the constructor of the class inside a
constructor to call another constructor.
T
h
e
 
o
n
l
y
 
w
a
y
 
o
f
 
c
a
l
l
i
n
g
 
a
n
o
t
h
e
r
 
c
o
n
s
t
r
u
c
t
o
r
 
w
i
t
h
i
n
 
a
 
c
o
n
s
t
r
u
c
t
o
r
 
i
s
t
h
r
o
u
g
h
 
u
s
i
n
g
 
t
h
i
s
 
k
e
y
w
o
r
d
.
27
 
 
 
 
 
A
n
y
 
Q
u
e
s
t
i
o
n
s
?
 
28
Slide Note

Copyright 2000 N. AYDIN. All rights reserved.

Embed
Share

Dive into the world of Object-Oriented Programming with a focus on key concepts like 'this' keyword usage, constructors, and method implementation in Java. Learn about the essential role of 'this' in accessing instance variables and how it refers to the current object. Explore a practical example of a Bank Account class to solidify your understanding.

  • OOP
  • Java
  • Constructor
  • Object Reference
  • Bank Account

Uploaded on Feb 25, 2025 | 1 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. this 2

  3. Outline this keyword this (first use case) this (second use case) Using this for constructor call 3

  4. this An important keyword used inside methods in order to refer to the current object. this is the calling object this is the object which called the method The object that occurs before the dot In sender: receiver.method(arguments) If sender and receiver are the same: method(arguments) [implicit version] this.method(arguments) [explicit version] 4

  5. Bank Account Public class Account { private int number; private double balance; private String currency; private double interestRate; public Account(int n, double b, String c){ number = n; if (b > 0) balance = b; else balance = 0; interestRate = 0; checksetCurrency(c); } public Account(int n, String c){ number = n; balance = 0; interestRate = 0; checksetCurrency(c); } public Account(int n){ number = n; balance = 0; currency = "TL"; interestRate = 0; } 5

  6. Bank Account public int getNumber() { return number; } public double getBalance() { return balance; } public String getCurrency() { return currency; } public boolean getInterestRate() { return interestRate; } public void setInterestRate(double i) { interestRate = i; } public void setCurrency(String c) { if (currency.equals("TL") && c.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && c.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || c.equals("USD")) { currency = c; } } 6

  7. this There are several use cases of this 1. It is used to access the instance variables without any confusion with parameter names. public void setInterestRate(double i) { interestRate = i; } public void setCurrency(String c) { if (currency.equals("TL") && c.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && c.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || c.equals("USD")) { currency = c; } 7

  8. this (first use case) public void setInterestRate(double i) { interestRate = i; } public void setCurrency(String c) { if (currency.equals("TL") && c.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && c.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || c.equals("USD")) { currency = c; } One letter parameters are not very descriptive. It is usually the convention in Java to use the instance variable name as parameters in the corresponding set methods. 8

  9. this (first use case) It should be smt like: public void setInterestRate(double interestRate) { interestRate = interestRate; } public void setCurrency(String currency) { if (currency.equals("TL") && currency.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && currency.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || currency.equals("USD")) { currency = currency; } parameter names are same with class instance names no compile error 9

  10. this (first use case) public void setInterestRate(double interestRate) { interestRate = interestRate; } public void setCurrency(String currency) { if (currency.equals("TL") && currency.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && currency.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || currency.equals("USD")) { currency = currency; } public static void main(String[] args) { Account account1 = new Account(1, 100, "TL"); account1.setCurrency("USD"); System.out.println(account1.getBalance()); System.out.println(account1.getCurrency()); account1.setInterestRate(0.02); System.out.println(account1.getInterestRate()); } What should be the output? 10

  11. this (first use case) public void setInterestRate(double interestRate) { interestRate = interestRate; } public void setCurrency(String currency) { if (currency.equals("TL") && currency.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && currency.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || currency.equals("USD")) { currency = currency; } When class instance and parameter have the same names, the assignment operation has no effect 11

  12. this (first use case) The parameters interestRate and currency shadow the class instances. public void setInterestRate(double interestRate) { interestRate = interestRate; The interestRate assigns the parameter interestRate to itself, causing no change whatsoever. In such cases you can use the this keyword to reach the class instances. public void setInterestRate(double interestRate) { this.interestRate = interestRate; 12

  13. this (first use case) public void setInterestRate(double interestRate) { interestRate = interestRate; } public void setCurrency(String currency) { if (currency.equals("TL") && currency.equals("USD")) { balance = balance / 32.88; } if (currency.equals("USD") && currency.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || currency.equals("USD")) { currency = currency; } public void setInterestRate(double interestRate) { this.interestRate = interestRate; } public void setCurrency(String currency) { if (this.currency.equals("TL") && currency.equals("USD")) { balance = balance / 32.88; } if (this.currency.equals("USD") && currency.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || currency.equals("USD")) { this.currency = currency; } 13

  14. this (first use case) public void setInterestRate(double interestRate) { this.interestRate = interestRate; } public void setCurrency(String currency) { if (this.currency.equals("TL") && currency.equals("USD")) { balance = balance / 32.88; } if (this.currency.equals("USD") && currency.equals ("TL")){ balance = balance * 32.88; } if (currency.equals("TL") || currency.equals("USD")) { this.currency = currency; } public static void main(String[] args) { Account account1 = new Account(1, 100, "TL"); account1.setCurrency("USD"); System.out.println(account1.getBalance()); System.out.println(account1.getCurrency()); account1.setInterestRate(0.02); System.out.println(account1.getInterestRate()); } What should be the output? 14

  15. this (first use case) It can be also used inside constructors to seperate class instances from parameters. // Constructors public Account(int n, double b, String c){ number = n; if (b > 0) balance = b; else balance = 0; interestRate = 0; checksetCurrency(c); } public Account(int n, String c){ number = n; balance = 0; interestRate = 0; checksetCurrency(c); } public Account(int n){ number = n; balance = 0; currency = "TL"; interestRate = 0; } 15

  16. First Constructor public Account(int n, double b, String c){ number = n; if (b > 0) balance = b; else balance = 0; interestRate = 0; checksetCurrency(c); } public Account(int number, double balance, String currency){ this.number = number; if (balance > 0) this.balance = balance; else this.balance = 0; this.interestRate = 0; this.checksetCurrency(currency); } 16

  17. Second Constructor public Account(int n, String c){ number = n; balance = 0; interestRate = 0; checksetCurrency(c); } public Account(int number, String currency){ this.number = number; this.balance = 0; this.interestRate = 0; checksetCurrency(currency); } 17

  18. Third Constructor public Account(int n){ number = n; balance = 0; currency = "TL"; interestRate = 0; } public Account(int number){ this.number = number; this.balance = 0; this.currency = "TL"; this.interestRate = 0; } 18

  19. this (first use case) To be consistent, the same can be done in get methods. public int getNumber() { return this.number; } public double getBalance() { return this.balance; } public String getCurrency() { return this.currency; } public boolean getInterestRate() { return this.interestRate; } 19

  20. A conceptual model for this A conceptual model for understanding this Each object in Java has an implicit instance variable, named this, that points to the object itself. 20

  21. A conceptual model for this This is not what really happens during execution. In Java, each object does NOT keep a reference to itself that would be a waste of memory space. However, take this as simply a conceptual model that helps us understand what this means. 21

  22. This (second use case) There are several use cases of this 1. It is used to access the instance variables without any confusion with parameter names. 2. It can be used to invoke constructors of the same class. When you have multiple constructors, this keyword can be used to call other constructors with a different set of arguments. 22

  23. Three Constructors private int number; private double balance; private String currency; // Constructors public Account(int number, double balance, String currency){ this.number = number; if (balance > 0) this.balance = balance; else this.balance = 0; this.checksetCurrency(currency); } public Account(int number, String currency){ this.number = number; this.balance = 0; checksetCurrency(currency); } public Account(int number){ this.number = number; this.balance = 0; this.currency = "TL"; } 23

  24. this (second use case) private int number; private double balance; private String currency; // Constructors public Account(int number, double balance, String currency){ this.number = number; if (balance > 0) this.balance = balance; else this.balance = 0; this.checksetCurrency(currency); } public Account(int number, String currency){ this(number, 0, currency); } public Account(int number){ this(number, 0, "TL"); } 24

  25. this (second use case) private int number; private double balance; private String currency; // Constructors public Account(int number, double balance, String currency){ this.number = number; if (balance > 0) this.balance = balance; else this.balance = 0; this.checksetCurrency(currency); } public Account(int number, String currency){ this(number, 0, currency); } public Account(int number){ this(number, 0, "TL"); } 25

  26. Using this for constructor call The constructor call should be the first statement in your constructor. public Account(int number, String currency){ int a; this(number, 0, currency); Otherwise, you will receive the following error! 26

  27. this public Account(int number, double balance, String currency){ this.number = number; if (balance > 0) this.balance = balance; else this.balance = 0; this.checksetCurrency(currency); } public Account(int number, String currency){ this(number, 0, currency); } public Account(int number){ Account(number, 0.0, "TL"); } You cannot use the constructor of the class inside a constructor to call another constructor. The only way of calling another constructor within a constructor is through using this keyword. 27

  28. Any Questions? 28

More Related Content

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