Inheritance
Product application
Product, Book, and Software classes
The code for the Product class
import java.text.NumberFormat; public class Product { private String code; private String description; private double price; protected static int count = 0; public Product() { code = ""; description = ""; price = 0; } public void setCode(String code) { this.code = code; } public String getCode() { return code; } public void setDescription(String description) { this.description = description; } public String getDescription() { return description; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public String getFormattedPrice() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format(price); } public String toString() { return "Code: " + code + "\n" + "Description: " + description + "\n" + "Price: " + this.getFormattedPrice() + "\n"; } public static int getCount() { return count; } }
The code for the Book class
public class Book extends Product { private String author; public Book() { super(); author = ""; count++; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return author; } public String toString() { return super.toString() + "Author: " + author + "\n"; } }
The code for the Software class
public class Software extends Product { private String version; public Software() { super(); version = ""; count++; } public void setVersion(String version) { this.version = version; } public String getVersion() { return version; } public String toString() { return super.toString() + "Version: " + version + "\n"; } }