Tag Archives: Examples

Java Tutorial : Implementing Interfaces – Example

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:

implementing inheritance example

Using Interface to Display the Number of Items Purchased

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:

implementing inheritance example 2

Number of Purchased CDs and Books

Java Tutorial : Inheritance In Java – Part 1

Java supports inheritance that enables a class to inherit data members and methods from another class. Inheritance enables you to reuse the functionalities and capabilities of the existing class by extending a new class from the existing class and adding new features to it.

Introduction to Inheritance

In inheritance, the class that inherits the data members and methods from another class is known as the subclass. The class from which the subclass inherits is known as the superclass. The superclass is also referred to as the base class, and the subclass is referred to as the derived class. You can create additional data members and methods to add more features in a subclass. A superclass can also be a subclass of another class.

The superclass represents the generalized properties, which are required for the hierarchy of the superclass. The subclasses represent specialization, in which data members and methods specific for the subclass are added to the inherited data members and the superclass methods.

Implementing Different Types of Inheritance

Java supports following types of inheritance:

  • Single Level Inheritance
  • Multilevel Inheritance

Single Level Inheritance

In single level inheritance, a subclass is derived from a single superclass. For example, subclasses B and C inherit the properties of a single superclass, A. The following figure shows the structure of single level inheritance:

single level inheritance

Single Level Inheritance

For example, the AirTicket class has data members and methods. Examples of data members are flight number, date, time, destination. The flightInfo() method of the AirTicket class displays information about a flight. An air ticket is of two types, confirmed ticket and requested ticket.

The ConfirmedTicket subclass that extends from the AirTicket class, inherits its data members. The ConfirmedTicket subclass contains seat number as an additional data member and the displaySeatInfo() method that displays the seat number of the confirmed ticket.

The RequestTicket subclass that extends from the AirTicket class also inherits its data members. The RequestTicket subclass contains status as an additional data member and the displayStatus() method that displays the status of the request ticket.

The following figure shows the concept of inheritance in the AirTicket class:

Concept of Inheritance

Concept of Inheritance