David Johnson is the owner of Safest Software Co. that deals in buying and selling of hardware and software computer products. Don Allen, an employee of the Safest Software Co. is assigned the task to develop a Java application to keep track of the inventory of the computer hardware and software products.
The application consists of an interface, ItemCount that includes the declaration of methods to be used in the superclass and subclass. The application includes the SoftwareProducts class that accepts the number of software items and calculates the
number.
The HardwareProducts class is included to accept the number of hardware items and to calculate the number. The Products class is included to create objects for the SoftwareProducts and HardwareProducts classes.
You can use the following code to create the ItemCount interface in Java:
interface ItemCount //Defining the interface.
{
//Defining abstract methods in the Billing interface.
public void noOfItems();
}
In the preceding code, the ItemCount interface contains the noOfItems() abstract method that needs to be implemented in the classes that implement the ItemCount interface.
You can use the following code to create the SoftwareProducts superclass:
class SoftwareProducts implements ItemCount
{
// Defining the data members.
int os= 60;
int oracle = 20;
int jav = 15;
int tot;
//Defining the methods.
public void calculate()
{
tot = os + oracle + jav;
}
public void noOfItems() // Method declared publicly in the interface.
{
System.out.println();
System.out.println(” “+”Total number of software products purchased is :
“+ tot);}
}
In the preceding code, the SoftwareProducts superclass defines the data members, os, oracle, and jav and the methods, calculate() and noOfItems(). These data members store the number of software products, Operating System, Oracle, and Java. The noOfItems() method displays the total number of the software items purchased by David.
You can use the following code to create the HardwareProducts class:
class HardwareProducts implements ItemCount
{
// Defining the data members.
int printer = 20;
int speaker = 12;
int harddisk = 25;
int htot;
public void calculate1()
{
htot = printer + speaker + harddisk;
}
public void noOfItems()// The method is declared publicly in the
interface, Billing.
{
System.out.println();
System.out.println(” “+”Total number of hardware items purchased is : ”
+htot);
}}
class Products
{
public static void main(String args[])
{
//Creating an object of the class, Hardware.
SoftwareProducts s = new SoftwareProducts();
HardwareProducts h = new HardwareProducts();
s.calculate();
h.calculate1();
s.noOfItems();
h.noOfItems();
}}
In the preceding code, the HardwareProducts subclass implements the ItemCount interface. The HardwareProducts class defines the data members, such as printer, speaker, and hard disk. These data members store the number of printer, speaker, and hard disk. The HardwareProducts class defines the calculate1() method to calculate the total number of hardware items. The noOfItems() method displays the total number of hardware items purchased by David.
The following figure shows the output of the preceding code:
Interfaces also enable you to declare constants that can be imported into multiple classes. The constant values declared in an interface can be implemented in any class. The constants defined in an interface are declared using the final keyword. You can use the following code to create the TBooks interface in Java:
interface TBooks
{
//Defining constants in an interface.
final int cd =10;
final int book =5;
}
In the preceding code, the TBooks interface declares a set of constants that stores the number of books and Compact Discs (CD) purchased.
You can use the following code to create the Dcd class:
class Dcd implements TBooks
{
void display()
{
System.out.println(” “);
System.out.println(“t The number of purchased CD’s are :” + cd);
System.out.println(” “);
}
}
In the preceding code, the Dcd class implements the TBooks interface to display the
number of purchased CDs.
You can use the following code to create the DBooks class:
class DBooks extends Dcd implements TBooks
{
void display1()
{
System.out.println(“t The number of purchased books are :” + book);
System.out.println(“t The number of purchased CD’s are :” + cd);
System.out.println(” “);
}
public static void main (String args[])
{
// Creating an instance of DBooks class.
DBooks db = new DBooks();
TBooks t;
t = db;
db.display1();
}}
In the preceding code, the DBooks subclass extends from the Dcd superclass and implements the TBooks interface. The display1() method displays the number of purchased books and CDs using inheritance.
The following figure shows the output of the preceding code:



