RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

lect03b
designing an interface; signatures
method stubs

We will talk about how somebody might want to use an BankAccount object (that is, what behaviors BankAccounts should have, which becomes “We'll make a class BankAccount; what methods should we have?”

We'll talk about the signature for a method; this will be the other thing you'll need to memorize:

typereturn name( parameter-type-and-name )

Stub functions

(If we have time:)

The problem with just writing, say,

public double getBalance() {
  ...
  }
is that the “...” doesn't actually compile. If you just want to check that your program (otherwise) compiles, you can replace the book's “...” with something which is syntatically correct (but is assuredly a logic-error). Since getBalance is contracted to return a double, we'll just return any ol' double:
  public double getBalance() {
    return -12.345;
    }
This satisfies the signature, so the program will compile. (Of course, it's wrong that every BankAccount has a balance of negative $12.345. That's fine, we'll worry about the correct code later; for now it's important that we double-check we don't have any silly syntax errors before we go any further.) This is called a stub method, because we have placed a stub return in place of the real return we'll use eventually.

Of course, if a method's signature says it returns a String, then our stub function must comply:

  public String getOwnerName() {
    return "John Jacob Jinklheimerschmidt";
    }

What about void methods, that don't return a value at all? You can leave those empty:

  public void deposit() {
    }

  // This compiles as-is.

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Feb.09 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme