Professional Documents
Culture Documents
Taxiiii
Taxiiii
The Stack
Object
func1
obj1 = 200 200
obj2 = null
main
x=3
Stack Heap
Assigning Object References
Reference can be assigned through new operator
obj2 = new Object();
Reference can assigned to another reference
obj2 = obj1;
WARNING
when assigning to another reference, both references now refer to the same
object
Assigning Object References
Example
Object obj1 = new Object();
Object obj2 = obj1;
Object
func1
obj1 = 200 200
obj2 = 200
main
x=3
Stack Heap
Simple Class
100
Stack Heap
Deep Clone
Copies fields and what they refer to
Must reimplement the clone() method
class Foo {
…
public Object clone() {
try {
Foo fobj = (Foo)super.clone(); // copies fields
fobj.nums = (int)nums.clone(); // arrays implement clone
return fobj;
} catch(CloneNotSupportedException e) { }
}
}
Inheritance
lets one class inherit fields and methods from another class
use keyword extends to explicitly inherit another classes
public and protected fields/methods
can only explicitly extend from one class
all classes implicitly extend the Object class
Inheritance
overriding a method
must have the same signature as original
declaring a method final means future derived classes cannot
override the method
overloading a method
method has same name but different signature
they are actually different methods
Inheritance
simple example
class Tester implements Foo, Bar {
...
}
Foo and Bar are interfaces
Tester must define all methods declared in Foo and Bar
Array Review