0% found this document useful (0 votes)
18 views4 pages

Inheritance

The document describes a superclass called Details that stores customer details and a subclass called Bill that calculates a customer's monthly phone bill based on their number of calls and different rate structures. The Bill subclass inherits from Details and overrides methods to calculate the amount due and display the customer details and amount. An example shows creating a Bill object and calling its methods.

Uploaded by

asha bobby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Inheritance

The document describes a superclass called Details that stores customer details and a subclass called Bill that calculates a customer's monthly phone bill based on their number of calls and different rate structures. The Bill subclass inherits from Details and overrides methods to calculate the amount due and display the customer details and amount. An example shows creating a Bill object and calling its methods.

Uploaded by

asha bobby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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( )
{
System.out.println("Name = "+name);
System.out.println("Address = "+address);
System.out.println("Telephone number = "+telno);
System.out.println("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( )
{
super.show( );
System.out.println("Amount to be paid= "+amt);
}
public void main( )
{
Page |4

Bill ob2=new Bill("ANN", "Blue Bell House", 9446467782, 50, 271);


ob2.calculate( );
ob2.show( );
}
}

You might also like