Java adapter

aratigadgil 1,899 views 8 slides Oct 27, 2015
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

JAVA Adapter class


Slide Content

JAVA
Adapter class
Prepared by
Miss. Arati A. Gadgil

2
Adapter class
It is one which contains null body definition for those methods which
are inheriting from appropriate Listener.
In java.awt.event.* we have Listener interface called WidowListener
which contains seven abstract methods. In the derived class implements
WindowListener interface; it is mandatory for derived class to define all
the methods even though the derived class is not required.
If the derived class wants to define the required methods, it has to
extend its corresponding adapter class called
java.awt.event.WindowAdapter and this class contains null body
definition for WindowListener interface methods.
Therefore which our Listener interface contains more than one
undefined method for that
Listener interfaces we have the appropriate adapter class whose general
notation is XXXAdapter.

public interface WindowListener
{
void windowActivated(WindowEvent e);
void windowClosed(WindowEvent e);
void windowClosing(WindowEvent e);
void windowDeactivated(WindowEvent e);
void windowDeiconified(WindowEvent e);
void windowIconified(WindowEvent e);
void windowOpened(WindowEvent e);
}
When implementing interface compulsory override all
methods.
3

Adapters are abstract classes for receiving various events. The
methods in these classes are empty. These classes exists as
convenience for creating listener objects.
4

import java.awt.*; import java.awt.event.*;
class frm12 extends Frame implements WindowListener
{ frm12()
{ super("window Listener");
addWindowListener(this);
setSize(300,300);
setVisible(true); }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e){System.exit(0);}
}
public static void main(String []a)
{ frm12 k=new frm12(); }
}
5

import java.awt.*;
import java.awt.event.*;
class wadptor extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
WindowListener wl=new wadptor();
Frame.addWindowListener(listener);
6

frame.addWindowListener(new wadptor());
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
7

Thank You
8