1. Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis, William Loftus, and Cara Cocking. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. *AP is a registered trademark of The College Entrance Examination Board which was not involved in the production of, and does not endorse, this product.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15. Static Methods Because it is static, the method can be invoked as: value = Helper.triple (5); public static int triple (int num) { int result; result = num * 3; return result; } class Helper
16.
17.
18.
19.
20.
21.
22.
23. Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (double value, char ch); public boolean doTheOther (int num); } None of the methods in an interface are given a definition (body) interface is a reserved word A semicolon immediately follows each method header
24.
25. Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition
42. Events and Listeners When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event Component This object may generate an event Listener This object waits for and responds to an event Event