J
jrefactors
Is this an example of program to interface?
1) Transaction t = new BankTransaction();
The following only uses the concrete class without the interface
2) BankTransaction t = new BankTransaction();
Is (1) a better choice than (2)? Why is that? (2) and (1) yield the
same output.
public interface Transaction
{
public void deposit(double amt);
}
public class BankTransaction implements Transaction
{
private double balance = 100;
public void deposit(double amt)
{ balance += amt;
}
}
public class BankTransactionTest
{ public static void main(String[] args)
{ Transaction t = new BankTransaction(); //program to interface??
//BankTransaction t = new BankTransaction();
t.deposit(200);
}
}
1) Transaction t = new BankTransaction();
The following only uses the concrete class without the interface
2) BankTransaction t = new BankTransaction();
Is (1) a better choice than (2)? Why is that? (2) and (1) yield the
same output.
public interface Transaction
{
public void deposit(double amt);
}
public class BankTransaction implements Transaction
{
private double balance = 100;
public void deposit(double amt)
{ balance += amt;
}
}
public class BankTransactionTest
{ public static void main(String[] args)
{ Transaction t = new BankTransaction(); //program to interface??
//BankTransaction t = new BankTransaction();
t.deposit(200);
}
}