I suggest that you write a similar example to validate the above rules in case of hiding of a static method.
Code: Select all
class Account{
double balance; double rate;
Account(double balance, double rate){
this.balance = balance;
this.rate = rate;
}
double getInterest( InterestCalculator ic, double yrs ){
try{
Number n = ic.computeInterest(balance, yrs, rate); //1
return n.doubleValue();
}catch(Exception e){
System.out.println(e);
//e.printStackTrace();
}
return 0.0;
}
}
class AccountManager{
public static void main(String[] args){
Account a = new Account(100, 0.2);
InterestCalculator sic = new InterestCalculator();
double sinterest = a.getInterest(sic, 3);
System.out.println("Straight Interest: "+sinterest);
InterestCalculator cic = new CompoundInterestCalculator();
double cinterest = a.getInterest(cic, 3);
System.out.println("Compound Interest: "+cinterest);
}
}
So I was wondering is there a way that instead of passing a reference to the getInterest method, pass the class name? Of course, Number n =ic.computeInterest(balance, yrs, rate); has to change to something like Number n =className.computeInterest(balance, yrs, rate);
Thanks