Download Interfaces-Java Network Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!
Java-Programming
Day-
Outline
• Interfaces
What? Why?
Creating Interfaces
Implementing interfaces
• Few Interfaces
• Event Handling
Java’s Event Model
Event Listener
Event Handling Techniques
java.awt.event package
Keyboad and Mouse Events
• Anonymous classes
Interfaces..cont..
One of the key features in software development
Various programs communicating with each other require standard
set of methods to be present
A software saying I have Printable interface implemented mean it
an communicate with printer
Example
EditAble interface
Methods
Cut Copy Paste
Constant
Clipboard capacity
Creating Interfaces
• Declaration
An interface is simply declared using keyword “ interface ”
interface selfService { -- - - - body of interface---}
Optionals
public, private, protected
Extends
Examples
interface selfService { - - - -body of interface - - - - - }
public interface selfService { - - - - body of interface - - - }
public interface selfPayment extends selfService {- - -body- -
Where do interface reside
• Interfaces follow almost same rules as that of classes
Public interfaces should be saved in a separate file with same file name
Interfaces other than public can be saved in any source file
There could be only one public interface in a source file others should
be non-public
A source file can contain an interface and a class.
Interface-Example
The BasicColors interface
public interface BasicColors{
String RED=“Red”;
String GREEN=“Green”;
String BLUE=“Blue” ;
public void changeToRed(); //method with no body
pubic void changeToGreen();
public void changeToBlue();
Save this interface to a file BasicColors.java
Compile it [you will get BasicColor.class]
Using interfaces..
- Few important concepts about interfaces
To use an interface the interface definition follow the rules of classes
Import an interface if it is in some other package
If a package is imported the interface are also imported like
classes
What if some methods in an interface are not required?
Provide an empty implementation
Example
public void ChangeToBlue(){}
Interface cannot contain variables
Interface constants are static this mean these can be used without
implementation
Example
BasicColors.RED
All method in an interface are by default public
Each interface is abstract by default
Using interfaces as abstract type
• Interfaces are good for using as abstract data type
• Often uses as parameter to a constructor or a method
Example
- public class BasicColorReader {
- public class BasicColorReader(){ }
- public void BasicColorReader(BasicColor obj){
- String colorInformation=obj.getColorInformation();
- System.out.println(“Color information = “+colorInformation);
- } }
An object of interface mean object of a class that implements that
constructor
Interfaces cont…
Pacakge: java.util
Methods
add(Object obj)
remove(Object obj)
Many other methods
Usage
Implemented by classes that like to server as a list abstract data
type
Package: java.awt.event
Methods: public void actionPerformed(ActionEvent e)
Used for listening button click events
Event Handling
- Events! Where do they come from
When a change in a program occur an even it generted
When ever a change occurs appropriate even object is created and passed either
to operating system or to some other event handler
Java intercepts events generated , translate them and route them to proper
event handler
Example
When a button of an applet is pressed A button press event object is generated Applet translates the event and try to find some handler in the applet If a handler in the program is found the event is handed over to it If no handler is found the default handler is used
Handling Event: detecting an Event and doing something useful
A class/Object must listen for some event
To listen an event it must register for listening itself for that event
Registration require implementation of proper event listener interface
Java provide a rich set of interfaces for events
A component must specify its event listener for its events to be processed
Creating our First Event Hander
Package required Classes and Listeners for awt component events are defined in java.awt.event package
- Creating an Applet public class myApplet extends Applet{ }
- What interface should we use to listen a button click The ActionListener interface
- Completing class declaration public class myApplet extends Applet implements ActionListener{ }
- Adding a button to our applet Button b=new Button(“Click It”); add(b);
- Specifying event listener for our button b.addActoinListener(this);
- Implementing ActionListener’s method public void actionPerformed(ActionEvent event){- - -code- - - }
Execution of the code
• Java registers MyApplet for Button Clicks
A component can use this class for listening its event
Whenever a buttonclick event is received by MyApplet it will execute its
actionPerformed() method
• Button b1 assigns its event listening to MyApplet
When ever this button b1 is clicked the event will handed over to
MyApplet by Java
• When a button is pressed
actionPerformed() method in MyApplet executed
Event handling using other classes
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{
private Button button1 = new Button();
MyListener listener=new MyListener();
public void init () {
button1.setText(“Click It");
button1.addActionListener(listener);
add(button1);
}
}
import java.awt.; import java.awt.event.; import java.applet.*; public class MyListener implements ActionListener {
//implementing method of ActionListener
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
}
Handling events for more than one button
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{
private Button button1 = new Button();
private Button button2 = new Button();
public void init () {
button1.setText(“Click It");
button1.addActionListener(this);
button2.addActionListener(this);
add(button2);
add(button1); }
//implementing method of ActionListener
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
} }