Skip to content

Commit 1d436db

Browse files
committed
OOP: Polymorphism
1 parent d96bc07 commit 1d436db

8 files changed

Lines changed: 81 additions & 1 deletion

File tree

objectorientedprogramming/polymorphism/BankAccount.java renamed to objectorientedprogramming/polymorphism/BankAccountDemo/v1/BankAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
public class BankAccount {
88
private String name;
9-
private double balance;
9+
protected double balance;
1010

1111
public String getName() {
1212
return this.name;

objectorientedprogramming/polymorphism/BankDemo.java renamed to objectorientedprogramming/polymorphism/BankAccountDemo/v1/BankDemo.java

File renamed without changes.

objectorientedprogramming/polymorphism/CurrentAccount.java renamed to objectorientedprogramming/polymorphism/BankAccountDemo/v1/CurrentAccount.java

File renamed without changes.

objectorientedprogramming/polymorphism/SavingsAccount.java renamed to objectorientedprogramming/polymorphism/BankAccountDemo/v1/SavingsAccount.java

File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Sample class BankAccount which is used as the super class of CurrentAcoount and SavingsAccount.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class BankAccount {
8+
private String name;
9+
protected double balance;
10+
11+
public String getName() {
12+
return this.name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
public void deposite(double amount) {
20+
this.balance += amount;
21+
}
22+
23+
public boolean withdraw(double amount) {
24+
if (balance > amount) {
25+
balance -= amount;
26+
return true;
27+
} else {
28+
return false;
29+
}
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Testing class for BankAccounts.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class BankDemo {
8+
public static void main(String[] args) {
9+
CurrentAccount c = new CurrentAccount();
10+
SavingsAccount s = new SavingsAccount();
11+
c.deposite(500.00);
12+
s.deposite(500.00);
13+
doWithdrawal(c); // Withdraw succeed.
14+
doWithdrawal(s); // Withdraw failed.
15+
}
16+
17+
public static void doWithdrawal(BankAccount acc) {
18+
boolean result = acc.withdraw(1000.00);
19+
if (result) {
20+
System.out.println("Withdraw succeed.");
21+
} else {
22+
System.out.println("Withdraw failed.");
23+
}
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* CurrentAccount allows to have negative balance.
3+
*
4+
* @version 2.0
5+
* @author L.Gobinath
6+
*/
7+
public class CurrentAccount extends BankAccount {
8+
/**
9+
* Overriding method.
10+
*/
11+
public boolean withdraw(double amount) {
12+
balance -= amount;
13+
return true;
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* SavingsAccount does not have any significant differences.
3+
*
4+
* @version 1.0
5+
* @author L.Gobinath
6+
*/
7+
public class SavingsAccount extends BankAccount {
8+
9+
}

0 commit comments

Comments
 (0)