...
| Code Block | ||
|---|---|---|
| ||
class SuperClass { //Main Bank class maintains all data during program execution
private double balance=0;
protected boolean withdraw(double amount) {
balance -= amount;
return true;
}
protected void overdraft() { //this method was added at a later date
balance += 300; //add 300 in case there is an overdraft
System.out.println("The balance is :" + balance);
}
}
class SubClass extends SuperClass { //all users hashave to subclass this to proceed
public boolean withdraw(double amount) {
// inputValidation();
// securityManagerCheck();
// Login by checking credentials using database and then call a method in SuperClass
// that updates the balance field to reflect current balance, other details
return true;
}
public void doLogic(SuperClass sc,double amount) {
sc.withdraw(amount);
}
}
public class Affect {
public static void main(String[] args) {
SuperClass sc = new SubClass(); //override
SubClass sub = new SubClass(); //need instance of SubClass to call methods
if(sc.withdraw(200.0)) { //validate and enforce security manager check
sc = new SuperClass(); //if allowed perform the withdrawal
sub.doLogic(sc, 200.0); //pass the instance of SuperClass to use it
}
else
System.out.println("You do not have permission/input validation failed!");
sc.overdraft(); //newly added method, has no security manager checks. Beware!
}
}
|
...