Menu
Topics Index
...
`

Icecream Prices Call Super Class Constructors - Java Example Program


Icecream Prices Call Super Class Constructors
class IceCreamPricesWithConstructors
{
    public static void main(String arg[])
    {
        IceCream ic = new IceCream("Pista", 2);
        System.out.println(ic.numberOfScoops + " scoops of " + ic.flavor + " flavor price is : " + ic.getPrice());
        
        FruitSaladWithIceCream fs = new FruitSaladWithIceCream("Chocolate", 1, 50);
        
        System.out.print(fs.gramsOfFruitSalad + " grams of fruit salad and ");
        System.out.println(fs.numberOfScoops + " scoops of " + fs.flavor + " flavor price is : " + fs.getPrice());
        
        KhubaniKaMeetaWithIceCream kkm = new KhubaniKaMeetaWithIceCream("Vanila", 1, 75);
        
        System.out.print(kkm.gramsOfKhubaniKaMeeta + " grams of khubani ka meeta and ");
        System.out.println(kkm.numberOfScoops + " scoops of " + kkm.flavor + " flavor price is : " + kkm.getPrice());
    
    }
}

class IceCream
{
    String flavor;
    int numberOfScoops;
    
    IceCream(String flavor, int numberOfScoops)
    {
        this.flavor = flavor;
        this.numberOfScoops = numberOfScoops;
    }
    
    double getPrice()
    {
        double pricePerScoop = 35.0;
        return numberOfScoops * pricePerScoop;
    }
}

class FruitSaladWithIceCream extends IceCream
{
    int gramsOfFruitSalad;

    FruitSaladWithIceCream(String flavor, int numberOfScoops, int gramsOfFruitSalad)
    {
        super(flavor, numberOfScoops); // LINE A
        this.gramsOfFruitSalad = gramsOfFruitSalad; // LINE B
    }

    double getPrice()
    {
        double iceCreamPrice = super.getPrice();
        double pricePerGram = 0.75;

        return gramsOfFruitSalad * pricePerGram + iceCreamPrice;
    }
}

class KhubaniKaMeetaWithIceCream extends IceCream
{
    int gramsOfKhubaniKaMeeta;

    KhubaniKaMeetaWithIceCream(String flavor, int numberOfScoops, int gramsOfKhubaniKaMeeta)
    {
        super(flavor, numberOfScoops);
        this.gramsOfKhubaniKaMeeta = gramsOfKhubaniKaMeeta;
    }

    double getPrice()
    {
        double iceCreamPrice = super.getPrice();
        double pricePerGram = 1.25;

        return gramsOfKhubaniKaMeeta * pricePerGram + iceCreamPrice;
    }
}
OUTPUT

2 scoops of Pista flavor price is : 70.0
50 grams of fruit salad and 1 scoops of Chocolate flavor price is : 72.5
75 grams of khubani ka meeta and 1 scoops of Vanila flavor price is : 128.75

DESCRIPTION

The program discussed in Method Overriding In Java is further changed to use the constructors. We use the super keyword to call the super-class constructor. Since we used constructors, the initialization became simpler and more readable.

THINGS TO TRY
  • Define a new class BadamMilkWithIceCream which extends from IceCream. Add member variable milliLitersOfBadamMilk and add a method getPrice which calls super.getPrice() to get the price of the ice cream. Create a constructor in this class and call the super-class constructor in it. Assume that the price of milli liter of badam milk is Rs. 0.20.
  • Exchange the lines LINE A and LINE B and observe the compilation error you get. Please note that when calling the super class constructor, the super keyword should be used only in the first line of the constructor.

© meritcampus 2019

All Rights Reserved.

Open In App