This article is the third article in the series of Object Oriented Programming tutorials, explaining the OOP concept - Inheritance. The same problem used in the Encapsulation tutorial is used here to explain Inheritance. The ABC bookshop needs software to maintain its billing procedures and stock management. This bookshop sells books, magazines, newsletters, and DVDs. In the last tutorial, we have developed a Book class.
public class Book {
private String title;
private String author;
private double price;
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
if (price > 0) {
this.price = price;
} else {
System.out.println("Invalid price");
}
}
public double getPrice() {
return this.price;
}
public void sell(int qty) {
System.out.println("Total: $" + (qty * price));
}
}
This time we are going to create a Magazine class with the following attributes and behaviors. Do not forget to use encapsulation when creating the class.