Using Java language....
Polymorphism of Interfaces :
Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here.
Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a monthly payment.
Make an Interface class called iMailable to print the monthly statements for all Accounts and all Carloans in the same print run. It should have one method in it called printStatement()
Finally, demonstrate polymorphism with interfaces by having both Account and Carloan implement iMailable and add code to each to have the printStatement() that prints out their data. Make an array of at least 2 iMailable and have at least one Account and at least one CarLoan in it, and use a loop to call printStatement() to show all the statements of the customers accounts and car loans in this array of the interface.
Below is a copy of the account class...
public class Account {
private String name; // instance variable
private double balance; // instance variable
// Account constructor that receives two parameters
public Account(String name, double balance) {
this.name = name; // assign name to instance variable name
// validate that the balance is greater than 0.0; if it
isn't
// instance variable balance keeps its default initial value of
0.0
if (balance > 0.0) { // if the balance is valid
this.balance = balance; // assign it to instance variable
balance
}
}
// method that deposits only a valid amount to the balance
public void withdrawal(double withdrawalAmount) {
if (withdrawalAmount <= balance) { // if the withdrawal amount
is valid subtract it from the balance
balance = balance - withdrawalAmount;
}
else {
System.out.printf("Withdrawal amount exceeded account
balance.");
}
}
// method returns the account balance
public double getBalance() {
return balance;
}
// method that sets the name
public void setName(String name) {
this.name = name;
}
// method that returns the name
public String getName() {
return name;
}
}
JAVA PROGRAM:
Account class (defined in my own term, as it was not in the question):
package test.polymorphsm;
//Account class
//It is generated with assumption of having 3 fields :
name,accountBalance and accountNumber
//It has a parametrized constructor
//The class implements the iMailable interface
public class Account implements iMailable{
private String name;
private Double accountBalance;
private Long accountNumber;
//Parameterized constructor: instance can be created through
this only
public Account(String name, Double accountBalance, Long
accountNumber) {
this.name = name;
this.accountBalance = accountBalance;
this.accountNumber = accountNumber;
}
//implements the method of the interface in its on way
@Override
public void printStatement() {
System.out.println("Given below is the print statement for an
ACCOUNT::");
System.out.println(this);//toString method will be used to print
the object's data
}
//overrides the toString method for the object
@Override
public String toString() {
return "Account [Name=" + name + ", accountBalance(in $)=" +
accountBalance + ", accountNumber=" + accountNumber
+ "]";
}
}
CarLoan class (defined as given in the question):
package test.polymorphsm;
//CarLoan class
//It is generated with having 4 fields : name,amountOwed ,rate and
monthlyPayment
//It has a parametrized constructor
//The class implements the iMailable interface
public class CarLoan implements iMailable{
private String Name;
private Double amountOwed;
private Double rate;
private Double monthlyPayment;
//Parameterized constructor: instance can be created through
this only
public CarLoan(String name, Double amountOwed, Double rate, Double
monthlyPayment) {
super();
Name = name;
this.amountOwed = amountOwed;
this.rate = rate;
this.monthlyPayment = monthlyPayment;
}
//implements the method of the interface in its on way
@Override
public void printStatement() {
System.out.println("Given below is the print statement for a
CARLOAN::");
System.out.println(this);//toString method will be used to print
the object's data
}
//overrides the toString method for the object
@Override
public String toString() {
return "CarLoan [Name=" + Name + ", amountOwed (in $)=" +
amountOwed + ", rate (in %)=" + rate + ", monthlyPayment(in
$)="
+ monthlyPayment + "]";
}
}
iMailable interface
package test.polymorphsm;
//This is an interface: it cannot be instantiated
//but other classes can implement it otr other interface can extend
it
public interface iMailable {
void printStatement(); //method is declared only: implementing classes has to define it within themselves
}
TestPolymorphism class (to test the polymorphism):
package test.polymorphsm;
public class TestPolymorphism {
public static void main(String[] args){
//2 Account instances are created and assigned to iMailable
type
//as Account class implements iMailable interface
iMailable acc1 = new Account("Thomas
Clark",1500.50,12054568L);
iMailable acc2 = new Account("Bobby Simpson",579.86,12050303L);
//2 CarLoan instances are created and assigned to iMailable
type
//as CarLoan class implements iMailable interface
iMailable cl1 = new CarLoan("Harry
George",10000.00,3.8,200.00);
iMailable cl2 = new CarLoan("Larry Gomes",5500.00,3.05,115.40);
//All of the above are added in a iMailable array
iMailable[] printArr = {acc1,cl1,cl2,acc2};
//traverse each element of the array and print the corresponding
types's printStatement.
//Depending on whether it is Account instance or CarLoan instance,
the printStatement() implementation
//of corresponding type will be called during runtime
for(iMailable recToPrint: printArr){
recToPrint.printStatement();
}
}
}
Output:
Given below is the print statement for an ACCOUNT::
Account [Name=Thomas Clark, accountBalance(in $)=1500.5,
accountNumber=12054568]
Given below is the print statement for a CARLOAN::
CarLoan [Name=Harry George, amountOwed (in $)=10000.0, rate (in
%)=3.8, monthlyPayment(in $)=200.0]
Given below is the print statement for a CARLOAN::
CarLoan [Name=Larry Gomes, amountOwed (in $)=5500.0, rate (in
%)=3.05, monthlyPayment(in $)=115.4]
Given below is the print statement for an ACCOUNT::
Account [Name=Bobby Simpson, accountBalance(in $)=579.86,
accountNumber=12050303]
Explanation:
Polymmorphism is defined as object's ability to take various firms. Here also, iMailable can be of type Account or of type CarLoan , and accordingly it has different implementation for its printStatement() method, which can be seen as we iterate over array elements. Whenever Account object is found , it' s defined printtatement() is printed, ans so as for CarLoan object.
Get Answers For Free
Most questions answered within 1 hours.