Today Quiz 5 interfaces exceptions best practices start on swing (if time) Homework hw3 due hw4 out (next thurs) S. Bowers 1 of 8
Implicit vs. Explicit Parameters Methods define their explicit parameters public void credit(double amt) the explicit parameter is amt When a method is called on an object it is passed as an implicit parameter Passed as a reference to the object the method is called on ( calling object ) Account a = new Account(100); a.credit(50); // like calling "credit(a, 50)" the implicit parameter here is the reference a Use the this keyword to access the implicit parameter public void credit(double amt) { this.balance += amt; there are times when this is really needed (more later) S. Bowers 2 of 8
Java Interfaces In Java, a class can subclass (extend) at most one other class Different than C++, which allows multiple inheritance Java provides interfaces as a workaround An interface is like a fully abstract class None of the methods are implemented Every class implementing an interface must implement all the methods Or else be declared as an abstract class public interface Account { public double getbalance(); public interface CreditAccount extends Account { public void credit(double amt); public interface DebitAccount extends Account { public void debit(double amt); public class TimeDeposit implements CreditAccount { public double getbalance() { S. Bowers 3 of 8
public class Checking implements CreditAccount, DebitAccount { public double getbalance() { public void credit(double amt) { public void debit(double amt) { Technically, all methods in a public interface are public But good practice to put public anyway Interfaces can be used as types! Account a = new TimeDeposit(300); System.out.println(a.getBalance()); Q: Why would we want to do this? allows for decoupling our code from any particular implementation allows for general-purpose methods (only require certain operations) Interfaces used throughout Java API, e.g., List is an interface Warning: Don t get carried away with interfaces! S. Bowers 4 of 8
More on Exceptions We can define our own methods to throw exceptions the throws keyword is part of the signature the throw keyword causing an exception to be thrown public void debit(double amt) throws Exception { if(balance - amt < 0) throw new Exception("Insufficient funds to debit amount."); balance -= amt; try { Account a = new Account(100); a.debit(200); catch(exception e) { e.printstacktrace(); Running this prints: java.lang.exception: Insufficient funds to debit amount. at Account.debit(Test.java:21) at Test.main(Test.java:6) S. Bowers 5 of 8
If your method throws an exception use the @throws tag: /** * * @param amt the amount to debit * @throws Exception If insufficient funds available */ Methods can throw multiple types of exceptions public void f() throws Type1Exception, Type2Exception, { Convention is to create your own exception classes: public class InsufficentFundsException extends Exception { public InsufficentFundsException(String msg) { super(msg); public void debit(double amt) throws InsufficientFundsException { usually this is all you do sometimes might add methods to pass additional information S. Bowers 6 of 8
Instead of catching exceptions, you can also punt // in checking public void debit(double amt) throws InsufficientFundsException { super.debit(amt); super.debit(5); // the fee // another example public static void main(string[] args) throws Exception { useful in some cases but your apps should catch the exceptions (e.g., in main) S. Bowers 7 of 8
Garbage Collection When you create a new object in Java The JVM allocates memory space on the heap All Java objects live in the heap When an object isn t needed, the JVM frees up the space for you This is done via automatic garbage collection The garbage collector (JVM) looks for unreachable objects Example: 1. Book b = new Book(); 2. Book c = new Book(); 3. Bood d = c; // d references same object as c 4. c = new Book(); // c now references a new object 5. d = c; // now there is a non-referenced object! 6. d = null; // d is now a "null" reference 7. d.gettitle(); // this generates a NullPointerException 8. b = null; // old b object can now be garb. collected Arrays of object references Q: What happens to the heap after this statement? Book[] books = new Book[3]; Each element in the books array is a null reference Q: What happens if we call: books[1].gettitle() We get a NullPointerException! S. Bowers 8 of 8