Interfaces

Interfaces - Advantages of Using Interfaces In Java What is...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Advantages of Using Interfaces In Java
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What is an Interface? In interface in Java is a file of method signatures . It's much like a Java source file for a program, except that the methods don't have any bodies - no implementation code! An interface file just shows the DECLARATIONS of the methods - the method "SIGNATURES". That is, the method name, the parameter types, the return type, and any exceptions the method throws. And instead of a left-brace right-brace block of code immediately below the method declaration line, we have instead a semicolon at the end of the method declaration line. And the keyword class on the class declaration line at the top of the source file is replaced by the keyword interface . One interface file used a lot in Java specifies only one method, and it looks like this: public interface Runnable { public void run(); } This source code would be in a Runnable.java file. We would compile it (to check the syntax for one thing!) and get a Runnable.class file. Once we have a .class file, that class name can be used as a type for an object! Interfaces are used all over the place in Java, so understanding how they work is important. The discussion below shows several common environments. (Our presentation target is to discuss the first 4 in class today, and the remaining 4 as they come up later in the course.) 1.Interfaces formally document an API between function providers and function users Let's say there are two software departments in our bank, one that develops "lobby" applications for tellers and managers, and one that develops the mainframe/server applications. We are about to create the teller application which will be run on PCs at the teller stations. The TellerStation application will communicate with another application called BankServices which runs on the server/mainframe. The BankServices application will be expanded to support the TellerStation application.
Background image of page 2
Since the two applications that will communicate with each other will be developed by different departments, it is a good idea to formally and jointly first create an interface file that will define the methods that the "front-end" TellerStation application will call on the "back- end" BankServices application. Here is a sample of the methods that would be defined in the interface file TellerSupport: public interface TellerSupport { public void deposit(String accountNumber, int amount) throws IllegalArgumentException, UnknownAccountException; public void withdraw(String accountNumber, int amount) throws IllegalArgumentException, UnknownAccountException, OverdraftException; public int getBalance(String accountNumber) throws UnknownAccountException; ... } The two departments would have a meeting (maybe just the chief programmers from each department) where the details of the methods would be hammered out: Is that an upper case "D" in withdraw() or a lower case "d"? Should the return values be void, or the new balance? Should the account number be int or String? Should the type of the
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Interfaces - Advantages of Using Interfaces In Java What is...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online