Java 212 Interfaces Intro to UML Diagrams UML
- Slides: 6
Java 212: Interfaces Intro to UML Diagrams
UML Class Diagram: class Rectangle • +/- refers to visibility • Color coding is not part of the UML diagram
Interfaces • Definition: A class that contains only abstract methods and/or named constants – Abstract methods are methods that have no body – The body for those methods should be written inside any class that “implements” the interface • Typically used as a software design parameter during the design phase of an application. • Also sometimes used as a workaround on Java’s limitation of not allowing multiple inheritance • Java allows classes to implement more than one interface – eg: If you want a class to respond to different kinds of events (Action. Event, Window. Event, Mouse. Event, etc), you can have a class implement all of these interfaces.
public interface Animal { public void speak(); public void eat(); } An Interface with two implemented classes public class Dog implements Animal { public void speak() { System. out. println("Woof"); } public void eat() { //code to display bone, kibbles } } public class Whale implements Animal { public void speak() { System. out. println("Squeak"); } public void eat() { //code to display little fish, plankton, etc } }
Interfaces In our discussion of GUIs, we discussed the use of inner (nested) classes to handle events. In these cases, we implemented the interface by creating a whole new inner class. However, we could just as easily declared an outer class as ‘implementing’ the interface. public class Rectangle. Calculator implements Action. Listener { … or even public class Rectangle. Calculator implements Action. Listener, Window. Listener, Mouse. Listener { //declares that this class implements three interfaces…
Two Interface Definitions: public interface Window. Listener { public void window. Opened(Window. Event e); public void window. Closing(Window. Event e); public void window. Closed(Window. Event e); public void window. Iconified(Window. Event e); public void window. Deiconified(Window. Event e); public void window. Activated(Window. Event e); public void window. Deactivated(Window. Event e); } public interface Action. Listener { public void action. Performed(Action. Event e); }