In: Computer Science
Inheritance - What is inheritance - Answer your own description in Readme.txt
Based on Hamburger project, you will create a package about Pizza.
In your Readme.txt, write how you make your Pizza package differently from the Hamburger package and also explain how inheritance work with your Pizza package.
package Hamburger;
/**
* Inheritance challenge – Hamburger place (Main, Hamburger, two
other Burger type class)
* Hamburger class should have name, bread roll type, meat, and up
to 4 additional
* additions(e.g. lettuce, tomato, carrot, etc)
* to select to be added to the burger. Each item will be charged an
additional
*/
public class Hamburger {
private String name;
//meat, price, breadRollType
private String meat;
private double price;
private String breadRollType;
private String additionName1;
private double additionPrice1;
private String additionName2;
private double additionPrice2;
private String additionName3;
private double additionPrice3;
private String additionName4;
private double additionPrice4;
public Hamburger(String name, String meat, double price, String
breadRollType) {
this.name = name;
this.meat = meat;
this.price = price;
this.breadRollType = breadRollType;
}
public void addHamburgerAddition1(String name, double
price){
this.additionName1 = name;
this.additionPrice1 = price;
}
public void addHamburgerAddition2(String name, double
price){
this.additionName2 = name;
this.additionPrice2 = price;
}
public void addHamburgerAddition3(String name, double
price){
this.additionName3 = name;
this.additionPrice3 = price;
}
public void addHamburgerAddition4(String name, double
price){
this.additionName4 = name;
this.additionPrice4 = price;
}
public double hamberPriceTotal(){
double hamburgerPrice = this.price;
System.out.println(this.name + " hambuger on a " + this.breadRollType + " roll with " + this.meat + "'s price is " + this.price);
if(this.additionName1 != null){
hamburgerPrice += this.additionPrice1;
System.out.println("Added " + this.additionName1 + " for an extra "
+ this.additionPrice1);
}
if(this.additionName2 != null){
hamburgerPrice += this.additionPrice2;
System.out.println("Added " + this.additionName2 + " for an extra "
+ this.additionPrice2);
}
if(this.additionName3 != null){
hamburgerPrice += this.additionPrice1;
System.out.println("Added " + this.additionName3 + " for an extra "
+ this.additionPrice3);
}
if(this.additionName4 != null){
hamburgerPrice += this.additionPrice4;
System.out.println("Added " + this.additionName4 + " for an extra "
+ this.additionPrice4);
}
return hamburgerPrice;
}
}
Inheritance can be defined as the process where one class acquires(inherits) the properties or attributes(methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
In the Pizza class we extend a separate class addition which has the variables additionName and additionPrice. These variables are inturn used by the pizza class when required.
addition.java
public class addition {
private String additionName;
private double additionPrice;
public addition(String additionName, double
additionPrice) {
super();
this.additionName =
additionName;
this.additionPrice =
additionPrice;
}
public addition() {
super();
}
public String getAdditionName() {
return additionName;
}
public void setAdditionName(String additionName)
{
this.additionName =
additionName;
}
public double getAdditionPrice() {
return additionPrice;
}
public void setAdditionPrice(double additionPrice)
{
this.additionPrice =
additionPrice;
}
}
Pizza.java
public class Pizza extends addition{
private String name;
//meat, price, breadRollType
private String meat;
private double price;
private String breadRollType;
public Pizza(String name, String meat, double price,
String breadRollType) {
this.name = name;
this.meat = meat;
this.price = price;
this.breadRollType = breadRollType;
}
public Pizza() {}
public double hamberPriceTotal(){
double PizzaPrice = this.price;
Pizza pizza = new Pizza();
System.out.println(this.name + " hambuger on a " +
this.breadRollType + " roll with " + this.meat + "'s price is " +
this.price);
if(pizza.getAdditionName() != null){
PizzaPrice += pizza.getAdditionPrice();
System.out.println("Added " + pizza.getAdditionName()
+ " for an extra " + pizza.getAdditionPrice());
}
return PizzaPrice;
}
}
This shows the property of inheritance and how its used in java. Inheritance ultimately makes the code more flexible and robust and also makes it easier for the code to be reused in any other package.