Similarly, you can use super
, introduced in
Segment 300, as a method target, if you want Java to ignore the
method defined in the target's class, using instead a method defined in a
superclass of the target.
For example, you can divide into two parts the work previously handled by
the describe
method defined in the Movie
class. One part of
the work is handled by a newly defined describe
method in the
Attraction
class:
public class Attraction { private int minutes; public int getMinutes() {return minutes;} public void setMinutes(int m) {minutes = m;} public Attraction () {minutes = 75;} public Attraction (int m) {minutes = m;} // Define describe public void describe () { System.out.println( " and lasts " + this.getMinutes() + " minutes" ); } }
And the other part of the work is handled by a redefined describe
method in the Movie class:
public class Movie extends Attraction { // Rest of Movie definition // Define rating: public int rating () { return script + acting + direction; } // Define describe public void describe () { System.out.print("Movie with rating " + this.rating()); super.describe(); } }