Inheritance
Inheritance
INHERITANCE
A superclass Detail has been designed to store the details of a customer. Define a
subclass Bill to compute the monthly telephone charge of the customer as per the
charge:
Number of calls Rate
1-100 Only rental charge
101-200 60 p per call + rental
201-300 80 p per call +rental
Above 300 ₹1 per call + rental
Member functions:
Detail(….)-parameterized constructor
void show( )-to display the customer details
Member functions:
Bill(……)-parameterized constructor
void calculate( )-to calculate the monthly telephone charge
void show( )-displays the details of the customer and the amount to be paid
Page |2
super(nm,a,t,r);
n=nn;
amt=0.0;
}
public void calculate( )
{
if (n>=1 && n<=100)
{
amt=rent;
}
else if (n>=101 && n<=200)
{
amt=rent+((n-100)*0.6);
}
else if (n>=201 && n<=300)
{
amt = rent+(100*0.6)+((n-200)*0.8);
}
else
{
amt = rent+(100*0.6)+(100*0.8)+((n-300)*1);
}
}
public void show( )
{
super.show( );
System.out.println("Amount to be paid= "+amt);
}
public void main( )
{
Page |4