Constructors in Inheritance - Super
As always, a sudden thought comes to our mind. We do save lot of time by reusing the
members and functions written in the parent class. Does the same apply to constructors?
You quickly learn there is a super keyword with which you can pass parameters and
invoke a constructor of the parent class. Common ! Lets translate it into code.
Create a class Customer with following private data members
Data Type |
Variable Name |
String |
name |
String |
address |
Integer |
age |
String |
mobileNumber |
Methods in
class Customer.
Method Name |
Function |
displayCustomer() |
This method displays the details of the customer with the
total bill amount and discount amount under privilege customer and senior
citizen customer |
Create a class SeniorCitizenCustomer which extends the class Customer. Use
super keyword to invoke parent class constructor.
Methods in
class SeniorCitizenCustomer
Method Name |
Function |
Return Type |
|
Include a 4 arguments constructor with the appropriate
arguments.The order in which the arguements should be passed is name,
address, age, mobileNumber. |
|
getBillAmount(amount) |
To calculate the payment amount
where discount is 12% of amount |
Double |
Create the class PrivilegeCustomer
which extends the class Customer . Use
super keyword to invoke parent class constructor.
Methods in
class PrivilegeCustomer
Method Name |
Function |
Return Type |
|
Include a 4 arguments constructor with the appropriate
arguments.The order in which the arguements should be passed is name,address,
age, mobileNumber. |
|
getBillAmount(amount) |
To calculate the payment amount
where discount is 30% of amount |
Double |
Create a driver class named Main which creates
an instance of the above
mentioned classes.
[All text in bold corresponds to input and the rest corresponds
to output.] Sample Input and Output
1:
Sample Input and Output 1:
1) Privilege Customer
2) Senior Citizen Customer
Enter Customer Type
1
Enter The Name
Ram
Enter The Age
25
Enter The Address
CBE
Enter The Mobile Number
9576531641
Enter The Purchased amount
5000
Bill Details
Name Ram
Mobile 9576531641
Age 25
Address CBE
Your bill amount is RS 5000.00 Your bill amount is discount under citizen customer You have to pay RS 3500.00
Problem
Requirements:
Java
Keyword |
Min Count |
Max
Count |
super |
2 |
- |
@MRProgrammer89
Solution :
package MrProgrammerBlog;
public class Customer {
private String name;
private String address;
private Integer age;
private String mobileNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public Customer (String name,String address,int age,String mobileNumber) {
this.name=name;
this.address=address;
this.age=age;
this.mobileNumber=mobileNumber;
}
public void displayCustomer() {
System.out.println("Bill Details");
System.out.println("Name "+getName());
System.out.println("Mobile "+getMobileNumber());
System.out.println("Age "+getAge());
System.out.println("Address "+getAddress());
}
}
package MrProgrammerBlog;
import java.text.DecimalFormat;
public class SeniorCitizenCustomer extends Customer{
DecimalFormat df = new DecimalFormat("0.00");
private double paymant_amount;
public SeniorCitizenCustomer(String name, String address, int age, String mobileNumber,double paymant_amount) {
super(name, address, age, mobileNumber);
this.paymant_amount= paymant_amount;
}
public double getPaymant_amount() {
return paymant_amount;
}
public void setPaymant_amount(int paymant_amount) {
this.paymant_amount = paymant_amount;
}
public void getBillAmount() {
double dfp = paymant_amount-((paymant_amount/100)*30);
System.out.println("Your bill amount is Rs "+df.format(paymant_amount)+". Your bill amount is discount under privilege customer");
System.out.println("You have to pay Rs "+df.format(dfp));
}
}
@MRProgrammer89
package MrProgrammerBlog;
import java.text.DecimalFormat;
public class PrivilegeCustomer extends Customer{
DecimalFormat df = new DecimalFormat("0.00");
private double paymant_amount;
public PrivilegeCustomer(String name, String address, int age, String mobileNumber,double paymant_amount) {
super(name, address, age, mobileNumber);
this.paymant_amount=paymant_amount;
}
public void getBillAmount() {
System.out.println("Your bill amount is Rs "+df.format(paymant_amount)+". Your bill amount is discount under privilege customer");
System.out.println("You have to pay Rs "+ df.format((paymant_amount-((paymant_amount/100)*12))));
}
}
@MRProgrammer89
package MrProgrammerBlog;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("1)Privilege Customer\n2)SeniorCitizen Customer");
System.out.println("Enter Customer Type");
int abc = sc.nextInt();
if(abc==1) {
System.out.println("Enter The Name");
String name =sc.next();
System.out.println("Enter The Age");
int age=sc.nextInt();
System.out.println("Enter The Address");
String address=sc.next();
System.out.println("Enter The Mobile Number");
String mono=sc.next();
System.out.println("Enter The Purchased Amount");
double amount=sc.nextDouble();
SeniorCitizenCustomer j1=new SeniorCitizenCustomer(name, address, age, mono, amount);
j1.displayCustomer();
j1.getBillAmount();
}
else if(abc==2) {
System.out.println("Enter The Name");
String name =sc.next();
System.out.println("Enter The Age");
int age=sc.nextInt();
System.out.println("Enter The Address");
String address=sc.next();
System.out.println("Enter The Mobile Number");
String mono=sc.next();
System.out.println("Enter The Purchased Amount");
double amount=sc.nextDouble();
PrivilegeCustomer j2=new PrivilegeCustomer(name,address,age,mono,amount);
j2.displayCustomer();
j2.getBillAmount();
}
else {
System.out.println("Invalid Customer Type");
}
}
}