Page |1
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
Class name : Detail
Data members:
name-to store the name
address-to store the address
telno-to store the telephone number
rent- to store the monthly rental charge
Member functions:
Detail(….)-parameterized constructor
void show( )-to display the customer details
Subclass name : Bill
Data members:
n-to store the number of calls
amt- to store the amount to be paid by the customer
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
public class Details
{
String name, address;
double telno;
double rent;
public Details(String nm, String a, double t, double r)
{
name=nm;
address=a;
telno=t;
rent=r;
}
public void show( )
{
[Link]("Name = "+name);
[Link]("Address = "+address);
[Link]("Telephone number = "+telno);
[Link]("Rent = "+rent);
}
}
public class Bill extends Details
{
int n;
double amt;
public Bill(String nm, String a, double t, double r, int nn)
{
Page |3
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( )
{
[Link]( );
[Link]("Amount to be paid= "+amt);
}
public void main( )
{
Page |4
Bill ob2=new Bill("ANN", "Blue Bell House", 9446467782, 50, 271);
[Link]( );
[Link]( );
}
}