Java Reference
In-Depth Information
Display 8.5
Copy Constructor Versus
clone
Method (part 2 of 2)
43
/**
44
Returns a safe copy of a. That is, if b is the
45
array returned, then b[i] is an independent copy of a[i].
46
*/
47
public static
Sale[] goodCopy(Sale[] a)
48 {
49 Sale[] b =
new
Sale[a.length];
50
for
(
int
i = 0; i < a.length; i++)
51 b[i] = a[i].clone( );
52
return
b;
53 }
54 }
Sample Dialogue
With copy constructors:
a[0] = atomic coffee mug Price and total cost = $130.0
b[0] = atomic coffee mug Price and total cost = $130.0
a[1] = invisible paint Price = $5.0 Discount 10.0%
Total cost = $4.5
b[1] = invisible paint Price and total cost = $5.0
The copy constructor
lost
the discount.
With clone method:
a[0] = atomic coffee mug Price and total cost = $130.0
b[0] = atomic coffee mug Price and total cost = $130.0
a[1] = invisible paint Price = $5.0 Discount 10.0%
Total cost = $4.5
b[1] = invisible paint Price = $5.0 Discount 10.0%
Total cost = $4.5
The
clone
method did
not lose the discount.
8.2
Abstract Classes
It is for us, the living, rather to be dedicated here to the unfinished work which
they who fought here have thus far so nobly advanced.
ABRAHAM LINCOLN,
Gettysburg address
An abstract class is a class that has some methods without complete definitions. You
cannot create an object using an abstract class constructor, but you can use an abstract
class as a base class to define a derived class.