Inheritance 3
Inheritance 3
[one_half]Number Of Calls
1 – 100
101 – 200
201 – 300
Above 300
[/one_half]
[one_half_last]Rate
[/one_half_last]
Member functions:
Member functions:
Bill(…) : parameterized constructor to assign values to data members of both classes and to initialize amt
= 0.0.
void cal() : calculates the monthly telephone charge as per the charge given above.
void show() : to display the detail of the customer and amount to be paid.
Specify the class Detail giving details of the constructor( ) and void show(). Using the concept of
inheritance, specify the class Bill giving details of the constructor( ), void cal() and void show().
[Note: We will be writing the main() function also in this program so as to familiarize the students on
how to run programs based on the concept of inheritance.]
Programming Code:
/**
* The superclass Detail stores the details of a customer and the subclass Bill calculates the telephone bill
* @author : www.javaforschool.com
*/
import java.io.*;
long telno;
double rent;
name = n1;
address = a1;
telno = t1;
rent = r1;
void show()
{
System.out.println(“Name of customer = “+name);
System.out.println(“Address = “+address);
int n;
double amt;
n = c;
amt = 0.0;
void cal()
if(n>=1 && n<=100) amt = rent; else if(n>=101 && n<=200) amt = 0.6*n + rent; else if(n>=201 &&
n<=300) amt = 0.8*n + rent; else amt = 1*n + rent; } void show() { super.show(); //calling the superclass
function show() System.out.println("No. of calls = "+n); System.out.println("Amount to be paid = Rs.
"+amt); } } //end of subclass Bill /* In your exams you don't need to write the below given code We are
writing it so as to familiarize the students on how to run programs based on the concept of
inheritance.*/ public class Question12_ISC2012 //Class which will contain the main() method and
execute the program { public static void main(String args[])throws IOException { BufferedReader br=new
BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the name : "); String
n1=br.readLine(); System.out.print("Enter the address : "); String a1=br.readLine();
System.out.print("Enter the telephone number : "); long t1=Long.parseLong(br.readLine());
System.out.print("Enter the monthly rental : "); double r1=Double.parseDouble(br.readLine());
System.out.print("Enter the number of calls : "); int c=Integer.parseInt(br.readLine()); Bill ob=new
Bill(n1,a1,t1,r1,c); //creating object of subclass System.out.println("*** Output ***"); ob.cal();
ob.show(); //calling show() function of subclass } }[/java]
Output: