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
In object-oriented programming, inheritance refers to the ability of an object to take on one or more characteristics from other classes of objects. The characteristics inherited are usually instance variables or member functions. An object that inherits these characteristics is known as a subclass. The object it inherits them from is known as a superclass. The details of how inheritance is implemented vary between languages, but the first language to implement it was Simula in 1967.
What is inheritance used for?
The purpose of inheritance is to consolidate and re-use code. For example, if the objects "car," "truck," and "motorcycle" are subclasses of vehicle, code applying to all of them can be consolidated into a vehicle superclass. The subclasses inherit this code and any future changes made to it, automatically.
Five types of inheritance
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;
}
}