CS 241 Lec 7 - 2
CS 241 Lec 7 - 2
CS 241: OBJECT
ORIENTED
PROGRAMMING
Dr. Ibrahim Eldesoky
[email protected]
2
The this() constructor call should be used to reuse the constructor in the
constructor. It maintains the chain between the constructors i.e. it is used for
constructor chaining. Let's see the example given below that displays the actual
use of this keyword.
12
public class T {
int x; // an instance variable
void m1( int x) { ... }
// x is shadowed by a parameter
void m2() { int x; ... }
} // x is shadowed by a local variable
Void m1 (int x) {
this.x = x;
// assign the value of the parameter x to the instance variable x
}
14
//---------------------------------------------------
// Sets up the account by defining its owner,
// account number, and initial balance.
//---------------------------------------------------
}
else
balance = balance + amount;
}
18
public withdraw (double amount) {
//accessors
public String getName () {
return name;
}
// overridden toString
public String toString () {
return acctNumber + "\t" + name +
"\t" + balance;
}
}// end class Account
20
acct1.deposit (25.85);
acct2.deposit(500.00);
System.out.println ();
System.out.println (acct1);
System.out.println (acct2);
System.out.println (acct3);
} // end of main
} // end of class BankAccount
22
• Pass by reference
• Pass by value
23
Parameters passing
Demo.java - Run
main()
a 8.0
b 11.0
sum -
product -
31
Demo.java - walkthrough
main()
add()
a 8.0
x 8.0
b 11.0
y 11.0
sum -
result -
product -
32
Demo.java - walkthrough
main() add()
a 8.0 x 8.0
y 11.0
b 11.0
result 19.0
sum 19.0
product -
33
Demo.java walkthrough
main() multiply()
a 8.0 x 8.0
b 11.0 y 11.0
sum 19.0
product -
34
Demo.java walkthrough
main()
multiply()
a 8.0
x 88.0
b 11.0
y 11.0
sum 19.0
product -
35
Demo.java - walkthrough
main()
multiply()
a 8.0 x 88.0
b 11.0 y 11.0
sum 19.0
product 88.0
36
Point Class
PassingReferences.java - run
PassingReferences- WalkThrough
main( )
Point
p
y: 10 x: 10
Notice that : Although variable p is part of the activation record for main(),
the memory for the Point object which it refers to is not part of the
activation record.
Java divides the memory allocated to a program into two parts – the stack
and the heap.
Activation records are maintained in the stack while objects in the heap.
40
PassingReferences- WalkThrough
•f(p) ; Method f() is invoked with p as the actual parameter.
•The invocation causes:
•the creation of an activation record for f ().
•The value of p is copied into the formal parameter v which is a
reference to a Point object (10,10). P and v refer the same object
•And the flow of control is passed to the method f.
main( )
Point
p
y: 10 x: 10
f( )
v
41
PassingReferences- WalkThrough
main( ) Point
p
y: 10 x: 10
f( ) Point
v
y: 0 x: 0
42
PassingReferences- WalkThrough
•The activation record for f() is then released and flow of control
is transferred back to the main() method where a println
statement is executed.
•The activation record for main() looks like the following:
main( )
Point
p
y: 10 x: 10
43
PassingReferences- WalkThrough
main( )
Point
p
y: 10 x: 10
g( )
v
44
PassingReferences- WalkThrough
public static void g (Point v) { v.setLocation(0,0); }
•Within method g() the Point instance method (v.setLocation(0,0)) is invoked on v. This
invocation causes an update to the Point object to which parameter v references. The update
changes (x,y) to (0, 0).
•Notice that this update changed neither actual parameter p nor formal parameter v.
However, the object to which they both refer has been updated and this update is visible
through either one of them (p & v are aliased).
•The activation records now look like those shown below:
main( )
Point
p
y: 0 x: 0
g( )