<< Chapter < Page | Chapter >> Page > |
Normally in an object-oriented program, all the
fields of an object are private and an accessor method like
getPrice()
is used to access the values of the fields.
If this is done, the computation of the price can be placed inthe accessor for the superclass and overridden in accessor for the
subclass.
Check this by executing the code and ensuring that the discounted price is computed.
The disadvantage of this solution is that the computation is
performed for each access of the field
price
.
Exercise Develop other solutions for this problem: (a) Call
computePrice
explicitly after the call to the constructor; (b)
Modify
getPrice
to compute the value of
price
on the first
call and save it for future calls. Summarize the advantages anddisadvantages of all the solutions for this problem.
Concept An object can contain fields of other user-defined objects, not just of primitive and predefined types. There is nodifference in the constructors, except that references to objects are passed as actual parameters and assigned to fields of the object.
Program: Constructor07.java
// Learning Object Constructor07
// constructors with object parametersclass Song {
String name; int seconds;
double pricePerSecond;
Song(String n, int s, double p) { name = n;
seconds = s; pricePerSecond = p;
}
public double computePrice() { return seconds * pricePerSecond;
}}
class SongSet {
public Song track1, track2;
public SongSet(Song t1, Song t2) { track1 = t1; track2 = t2;
}}
public class Constructor07 {
public static void main(/*String[] args*/) {
Song song1 = new Song("Waterloo", 164, 0.01); Song song2 = new Song("Fernando", 253, 0.01);
SongSet set = new SongSet(song1, song2); double price1 = set.track1.computePrice();
double price2 = set.track2.computePrice(); }
}
Two objects of type
Song
are allocated and
assigned to fields of another object of type
SongSet
which has two fields of type
Song
.
Song
are
allocated and their references assigned to the variables
song1
and
song2
.
(You may want to select
Animation / Run Until (ctrl-T)
to skip the
animation of these declarations.)set
is allocated. An object of type
SongSet
is allocated with default null fields.SongSet
is called and the references in the two variables
song1
and
song2
are passed as actual parameters. These references
are stored in the two fields
track1
and
track2
.SongSet
is returned and stored in
set
.price1
and
price2
.
set
is an object
of type
Songset
, while
set.track1
is an object of type
Song
and thus can be used to call the method
computePrice
.
Exercise Modify the program so that the variables
song1
and
song2
are not used; instead, the constructors for the songs are
embedded within the constructor call for
SongSet
.
Exercise Modify the program so the constructors for the songs are
call within the constructor for
SongSet
. Under what circumstances
would this be done?
Concept An object of a subclass is also an object of the type of the superclass. Therefore, it can be used when an actual parameter is expected.
Program: Constructor08.java
// Learning Object Constructor08
// constructors with subclass object parametersclass Song {
String name; int seconds;
double pricePerSecond;
Song(String n, int s, double p) { name = n;
seconds = s; pricePerSecond = p;
}
public double computePrice() { return seconds * pricePerSecond;
}}
class DiscountSong extends Song {
double discount;
DiscountSong(String n, int s, double p, double d) { super(n, s, p);
discount = d; }
public double computePrice() {
return seconds * pricePerSecond * discount; }
}
class SongSet { public Song track1, track2;
public SongSet(Song t1, Song t2) {
track1 = t1; track2 = t2; }
}
public class Constructor08 { public static void main(/*String[] args*/) { Song song1 = new Song("Waterloo", 164, 0.01);
DiscountSong song2 = new DiscountSong("Fernando", 253, 0.01, 0.8); SongSet set = new SongSet(song1, song2);
double price1 = set.track1.computePrice(); double price2 = set.track2.computePrice();
}}
We allocate two objects, one of type
Song
and
one of type
DiscountSong
, and use them as actual parameters in
the constructor for an object of type
SongSet
that expects two
parameters of type
Song
.
Song
the other of type
DiscountSong
are
allocated and their references assigned to the variables
song1
and
song2
,
respectively.(You may want to select
Animation / Run Until (ctrl-T)
to skip the
animation of these declarations.)set
is allocated, and an object of type
SongSet
is allocated with default null fields.SongSet
is called and the references in the two variables
song1
and
song2
are passed as actual parameters. These references
are stored in the two fields
track1
and
track2
.SongSet
is returned and stored in
set
.price1
and
price2
.
set
is an object
of type
Songset
, while
set.track1
is an object of type
Song
and thus can be used to call the method
computePrice
of that class. Similarly for
price2
,
except that
set.track2
is an object of type
DiscountSong
; check that the method
computePrice
of this
class is called.
Exercise Can
s2
in the main method be declared to be of type
Song
? Explain.
Notification Switch
Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?