![]() |
![]() |
![]() |
![]() |
![]() |
|
Now you can write the program with one version of rating defined as
an instance method of the Movie class, as in Segment 194,
and another version defined as an instance method of the Symphony
class:
public class Symphony {
public int music, playing, conducting;
public int rating () {
return music + playing + conducting;
}
}
Java picks the method defined in the class of the target instance, as shown in the following example:
public class Demonstrate {
public static void main (String argv[]) {
Movie m = new Movie();
m.script = 8; m.acting = 9; m.direction = 6;
Symphony s = new Symphony();
s.music = 7; s.playing = 8; s.conducting = 5;
System.out.println("The rating of the movie is " + m.rating());
System.out.println("The rating of the symphony is " + s.rating());
}
}
--- Result ---
The rating of the movie is 23
The rating of the symphony is 20