<< Chapter < Page Chapter >> Page >

The website wants to sell certain songs at a discount. The subclass DiscountSong inherits from class Song , adds a field discount and overrides computePrice to include discount in the computation. The constructor for the subclass calls the three-parameter constructor forthe superclass, passing it the three parameters that it expects. The fourth parameter is used directly in the constructor DiscountSong to initialize the field discount .

  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the five fields of the object of the subclass DiscountSong and default values are assigned to the fields. Four fields inherited from the superclass and one field discount added by the subclass.
  • The constructor for the subclass DiscountSong is called with four parameters. It calls the constructor for the superclass Song which assigns values to three fields from the parameters and the fourth by calling computePrice .
  • The superclass constructor returns and then the fourth parameter of the subclass constructor is assigned to the field discount .
  • The reference to the subclass object is returned and assigned to a variable song1 of that type.

Unfortunately, this does not do what we intended, because the superclass method for computePrice is used to compute price instead of the method from the subclass.

Exercise Could song1 be declared to be of type Song ? Explain your answer.

Program: Constructor06B.java

// Learning Object Constructor06B //    constructors for subclassesclass Song {     String name;    int seconds;     double pricePerSecond;    double price;      Song(String n, int s, double p) {         name = n;        seconds = s;         pricePerSecond = p;        price = computePrice();     }      private 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;        price = computePrice();     }      private double computePrice() {        return seconds * pricePerSecond * discount;     }}  public class Constructor06B {     public static void main(/*String[] args*/) {         DiscountSong song1 = new DiscountSong("Waterloo", 164, 0.01, 0.8);        double price = song1.price;     }}

The problem can be solved by adding a call to computePrice in the constructor for the subclass.

Check this by executing the code and ensuring that the discounted price is computed.

The disadvantage of this solution is that we are calling computePrice twice.

Program: Constructor06C.java

// Learning Object Constructor06C //    constructors for subclassesclass Song {     String name;    int seconds;     double pricePerSecond;      Song(String n, int s, double p) {        name = n;         seconds = s;        pricePerSecond = p;     }      public double getPrice() {        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 getPrice() {         return seconds * pricePerSecond * discount;    } }  public class Constructor06C {    public static void main(/*String[] args*/) {        Song song1 = new DiscountSong("Waterloo", 164, 0.01, 0.8);         double price = song1.getPrice();    } }

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask