This program shows another example how inheritance can be used to remove duplicate code and group related classes.
Icecream Prices
class IcecreamPrices
{
public static void main(String arg[])
{
IceCream ic = new IceCream();
ic.flavor = "Pista";
ic.numberOfScoops = 2;
System.out.println(ic.numberOfScoops + " scoops of " + ic.flavor + " flavor price is : " + ic.getIceCreamPrice());
FruitSaladWithIceCream fs = new FruitSaladWithIceCream();
fs.flavor = "Chocolate";
fs.numberOfScoops = 1;
fs.gramsOfFruitSalad = 50;
System.out.print(fs.gramsOfFruitSalad + " grams of fruit salad and ");
System.out.println(fs.numberOfScoops + " scoops of " + fs.flavor + " flavor price is : " + fs.getFruitSaladWithIceCreamPrice());
KhubaniKaMeetaWithIceCream kkm = new KhubaniKaMeetaWithIceCream();
kkm.flavor = "Vanila";
kkm.numberOfScoops = 1;
kkm.gramsOfKhubaniKaMeeta = 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.getKhubaniKaMeetaWithIceCreamPrice());
}
}
class IceCream
{
String flavor;
int numberOfScoops;
double getIceCreamPrice()
{
double pricePerScoop = 35.0;
return numberOfScoops * pricePerScoop;
}
}
class FruitSaladWithIceCream extends IceCream
{
int gramsOfFruitSalad;
double getFruitSaladWithIceCreamPrice()
{
double iceCreamPrice = getIceCreamPrice();
double pricePerGram = 0.75;
return gramsOfFruitSalad * pricePerGram + iceCreamPrice;
}
}
class KhubaniKaMeetaWithIceCream extends IceCream
{
int gramsOfKhubaniKaMeeta;
double getKhubaniKaMeetaWithIceCreamPrice()
{
double iceCreamPrice = getIceCreamPrice();
double pricePerGram = 1.25;
return gramsOfKhubaniKaMeeta * pricePerGram + iceCreamPrice;
}
}
OUTPUT2 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
DESCRIPTIONHere we have created three classes - IceCream
, FruitSaladWithIceCream
and KhubaniKaMeetaWithIceCream
and implemented the methods which provide the price of each item. The method getIceCreamPrice
in IceCream
class gives the price of the ice cream. The method getFruitSaladWithIceCreamPrice
in FruitSaladWithIceCream
class gives the price of ice cream and fruit salad. This method internally calls getIceCreamPrice
to get the ice cream price and adds it to the price of the fruit salad. Similarly the method getKhubaniKaMeetaWithIceCreamPrice
in KhubaniKaMeetaWithIceCream
class gives the price of khubani ka meeta and ice cream.
THINGS TO TRY
- Define a new class
BadamMilkWithIceCream
and add member variable milliLitersOfBadamMilk
and add a method getBadamMilkWithIceCreamPrice
. Assume that the price of milli liter of badam milk is Rs. 0.20.
The above program shows how we can use inheritance to group related classes and avoid duplicate code. The only problem with this program is that the method names
getIceCreamPrice
,
getFruitSaladWithIceCreamPrice
and
getKhubaniKaMeetaWithIceCreamPrice
have the class name included in them. It would have been better if we could have only one method
getPrice
and all these classes implement that method. Depending upon the type of object, the corresponding method is called and the price is returned. How we can do this is explained in
Method Overriding In Java.