A super class Stock has been defined to store the details of the stock of a retail store. Define a subclass Sales to store the details of the item sold and update the stock.
The details of the members of both the classes are given below:
Class name: Stock
Data members/instance variables:
item: to store the name of the item
qty: to store the quantity of an item in stock
Methods/Member functions:
Stock(…): parameterized constructor to assign values to the data members
void display(): to display the stock details
Class name: Sales
Data members/instance variables:
sqty: to store the quantity sold
Methods/Member functions:
Sales(…): parameterized constructor to assign values to the data members of both the classes
void update(): to update stock by reducing the sold quantity from the stock quantity
void display(): to display the stock details before and after updating
Assume that the super class Stock has been defined. Using the concept of inheritance, specify the class Sales giving the details of the constructor(…), void update() and void display().
public class Stock
{
public String item;
public int qty;
Stock(String item,int qty){
this.item=item;
this.qty=qty;
}
void display()
{
System.out.println("Item name:"+item);
System.out.println("Quantity:"+qty);
}
}
public class Sales extends Stock
{
public int sqty;
Sales(String item,int qty,int sqty)
{
super(item,qty);
this.sqty=sqty;
}
void update()
{
qty=qty-sqty;
}
void display()
{
System.out.println("Stock Item:"+item);
System.out.println("Before update:");
System.out.println("Stock Quantity:"+qty);
update(); //call the update method
System.out.println("After update");
System.out.println("Current Stock Quantity:"+qty);
}
}