object oriented programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
Object Oriented Programming PowerPoint Presentation
Download Presentation
Object Oriented Programming

Object Oriented Programming

8 Vues Download Presentation
Télécharger la présentation

Object Oriented Programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Object Oriented Programming

  2. Topic to be covered today • Interface • Event Handling

  3. Interface • Using interface, we specify what a class must do, but not how it does this. • An interface is syntactically similar to a class, but it lacks instance variables and its methods are declared without any body. • An interface is defined with an interface keyword.

  4. General Format

  5. Interface Contdd….. • Two types of access: • public – interface may be used anywhere in a program • default – interface may be used in the current package only • Interface methods have no bodies – they end with the semicolon after the parameter list. They are essentially abstract methods. • An interface may include variables, but they must be final, static and initialized with a constant value. • In a public interface, all members are implicitly public.

  6. Interface Implementation • A class implements an interface if it provides a complete set of methods defined by this interface. • any number of classes may implement an interface • one class may implement any number of interfaces • Each class is free to determine the details of its implementation. • Implementation relation is written with the implements keyword.

  7. Example • Declaration of the Callback interface: • Client class implements the Callback interface:

  8. Events • An event is an action initiated by the user interacting with the program. • Examples • Keyboard events - pressing a key, holding a key, releasing a key • Mouse events - moving the mouse, clicking the mouse • GUI events - clicking on a button, resizing a window, closing a window, opening a window • An event in Java is an object of a particular event class, that represents some user actions to which the GUI might respond

  9. Low Level Events • Low level events represent direct communication from the user • Low level event examples (all the event classes listed below belong to the java.awt.event package0: • key event - a keyboard key pressed or released - in the KeyEvent class • focus event– a component got focus, lost focus – in the FocusEvent class • mouse event- the mouse is moved or dragged, a mouse button is pressed or released, the mouse cursor enters or exits a component - in the MouseEvent class • component event - a component is hidden, shown, resized, or moved – in the ComponentEvent class • container event - a component is added to or removed from a container in the ContainerEvent class • window event- a window is opened, closed, activated, deactivated, etc. - in the WindowEvent class

  10. High Level Events • High level events usually involve one or more low level events • High Level Event examples • action event - do a command – ActionEvent class • adjustment event - represents scrollbar motions such as a value was adjusted – AdjustmentEvent class • item event - occurs when the user selects a checkbox, choice, or list item, i.e. item state has changed –ItemEvent class • text event– represents a text component content (value) change – TextEvent class

  11. How Do the Low and High Level Events Work in Java? • When the user clicks the mouse on a button, then releases it, the button gets two or three separate, low level mouse events • one for mouse down • one for mouse up • possibly one for mouse drag (if the user moves the mouse while the button is pressed) • However, the button then fires one high level event only - ActionEvent

  12. Event Hierarchy • Events are organized into hierarchy of event classes • Event classes contain data relevant to a particular event type • An event is an object of one of the event classes

  13. TextEvent WindowEvent InputEvent FocusEvent PaintEvent java.util.EventObject java.awt.AWTEvent AdjustmentEvent ContainerEvent MouseEvent KeyEvent java.lang.Object ComponentEvent ActionEvent ItemEvent Event Hierarchy in Java

  14. Event Sources • The type of an event depends on its source • Example of event sources: • the keyboard • the mouse • the GUI components – buttons, text fields, windows • Event source is an object with the ability to determine when an event has occurred

  15. Event Driven Programming • In event driven programming the events “drive” the execution of the program, e.g. the code is executed when events are activated • The program interacts with the user and generates events based on the external user actions • Java Visual (Graphical) programming and Visual Basic programming are event driven • When writing applets that are using events in Java we have to import the “events” package java.awt.event.*;

  16. Java Event Delegation Model • Java uses delegation-based modelfor event handling • Java uses event listener to register an event and event handler to respond to the event • The use of event listeners in event handling is called delegation event model

  17. Java Event Delegation Model • An external user’s action on a source object(e.g. the event source) activates an event • An event listenerobject (e.g. an object interested in the event source) receives the event. This object is an instance of a class that implements a specific EventListener interface • Example: ActionEvent --> ActionListener • The source maintains a list containing all the listener objects that have registered to be notified of events of that type

  18. Java Event Delegation Model • The transmission of an event from an event sourceto an event listenerinvolves invoking a method on the listener object by which the sourcenotifies the listenerof the occurrence of an event of a specific type • Example: method actionPerformed (ActionEvent e) • An EventListener interface declares one or more methods which must be defined in the listener class, and which are invoked by the event source in response to each specific event type handled by the interface • Example: EventListener method actionPerformed (ActionEvent e)

  19. Event Handling Components

  20. Example

  21. Events and Listeners • The Java standard class library contains several classes that represent typical events • Components, such as an applet or a button, generate (fire) an event when it occurs • Objects, called listeners, wait for events to occur. A listener object is an instance of a class that implements a specific listener interface • A number of listener interfaces are pre-defined and each interface declares the appropriate methods for a specific class of events

  22. Event Component Listener This object may generate an event This object waits for and responds to an event Events and Listeners When an event occurs, the component calls the appropriate method of the listener, passing an (event) object that describes the event

  23. event listener 1 event listener 2 event listener 3 event source Events and Listeners • Each event is represented by an object that gives information about the event and identifies the event source. • Each event source can have multiple listeners registered on it. A single listener can register with multiple event sources.

  24. Event Listeners • A listener object can be registered on a source object to be notified of the occurrence of all events of the specific class for which the listener object is designed • The occurrence of an event defined by the specified class will automatically invoke the matching method in the listener object • The code in the body of the method is designed by the programmer to perform the desired action when the event occurs

  25. Listener Interfaces • We can create a listener object by writing a class that implements a particular listener interface • The Java standard class library contains several interfaces that correspond to particular event categories • After creating the listener, we add the listener to the component that might generate the event to set up a relationship between the component, generating the event and the event listener

  26. Processing an Event in Java • Register an event listener • “listens” for events generated by GUI components • an object of a class from the package java.awt.event • Implement anevent handler • a method that is automatically called in response to a particular type of event

  27. Processing an Event in Java • For each event class there is a corresponding listener interface defined in Java and corresponding listener methods (handlers) in the listener interface • Example : • for the event class ActionEvent • the listener is ActionListener • and the listener method (handler) is actionPerformed (ActionEvent e )

  28. Example: AWT Event Listeners • Commonly used AWT event Listeners • ActionListener • AdjustmentListener • FocusListener • ItemListener • KeyListener • MouseListener • MouseMotionListener • MouseWheelListener • WindowListener • WindowFocusListener • WindowStateListener

  29. How to Implement an Event Handler in Java • Every event handler requires three separate steps • In the declaration for the event handler class, we specify that the class either implements a listener interface or extends a class that implements a listener interface public class MyClass implements ActionListener { … } • Code that registers an instance of the event handler class as a listener upon one or more components someComponent.addActionListener(instanceOfMyClass); • Code that implements the methods in the listener interface public void actionPerformed(ActionEvent e) { //code that reacts to the action... }

  30. Summary of Event Source

  31. Summary of Listener Interfaces

  32. Adapter Classes • Adapter Class exists as convenience for creating a listener object. • Extend this class to create a listener for a particular listener interface and override the methods for the events of interest. • It defines null methods for all of the methods in the listener interface, so you can only have to define methods for events you care about. • Commonly used adapter classes: • FocusAdapter • KeyAdapter • MouseAdapter • MouseMotionAdapter • WindowAdapter