Menu
Topics Index
...
`


Methods Overiding, Overloading >
Siva Nookala - 15 Mar 2016
The other use of super keyword is to call the super-class constructors from the sub-class constructor. We can change the Ice Cream Prices program discussed in Method Overriding In Java to use the super keyword to call the super-class constructors.

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.
One difference between using super keyword to call the super-class methods and using super keyword to call the super-class constructors is that when calling the super-class methods, there is no restriction on where and how many times we can call it, but in case of super-class constructor we can only call it once and it has to be in the first line of the class.
class A
{
    int i;
    A(int i)
    {
        this.i = i;
    }
}

class B extends A
{
    int j;
    B(int i, int j)
    {
        super(i);
        this.j = j;
    }
}

class C extends A
{
    int k;
    C(int i, int k)
    {
        this.k = k; // THIS IS INVALID AND WILL THROW COMPILATION ERROR
        super(i);
    }
}
Inheritance And Constructors In Java explains in detail the various ways in which the super-class constructors can be called.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App