Java AWT Adapter Classes

Java AWT Adapter Classes
Slide Note
Embed
Share

Java AWT adapter classes are abstract classes in the java.awt.event package that simplify event handling by providing empty implementations for multiple methods in listener interfaces. By extending adapter classes, developers can implement only the necessary event handling methods. Inner classes in Java act as a security mechanism and enable encapsulation within other classes, providing access control for nested classes.

  • Java
  • AWT
  • Adapter Classes
  • Event Handling
  • Inner Classes

Uploaded on Mar 06, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Adapter Class Java AWT Adapters are abstract classes from java.awt.event package. Every listener that includes more than one abstract method has got a corresponding adapter class. The adapter classes are very special classes that are used to make event handling very easy. There are listener interfaces that have many methods for event handling and we know that by implementing an interface we have to implement all the methods of that interface. But sometimes we need only one or some methods of the interface. In that case, Adapter classes are the best solution. SACHIN KHARADE

  2. For example, the MouseListener interface has five methods: mouseClicked(), mouseEntered(), mouseExited(), mousePressed() and mouseReleased(). If in your program, you just need two events: mouseEntered() and mouseExited() that time you can use adapter class for the mouseListener interface. The adpter classes contain an empty implementation for each method of the event listener interface. To use the adapter class, you have to extend that adapter class. SACHIN KHARADE

  3. Listener Interface Adapter Class ComponentListener ComponentAdapter ContainerListener ContainerAdapter FocusListener FocusAdapter KeyListener KeyAdapter MouseListener MouseAdapter MouseMotionListener MouseMotionAdapter WindowListener WindowAdapter SACHIN KHARADE

  4. Here's a mouse adapter that beeps when the mouse is clicked import java.awt.*; import java.awt.event.*; public class MouseBeeper extends MouseAdapter { public void mouseClicked(MouseEvent evt) { Toolkit.getDefaultToolkit().beep(); } } Without extending the MouseAdapter MouseAdapter class, I would have had to write the same class like this import java.awt.*; import java.awt.event.*; public class MouseBeeper implements MouseListener { public void mouseClicked(MouseEvent evt) { Toolkit.getDefaultToolkit().beep(); } public void mousePressed(MouseEvent evt) {} public void mouseReleased(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseExited(MouseEvent evt) {} } SACHIN KHARADE

  5. Inner Classes Inner classes are a security mechanism in Java. Inner class is a class defined inside other class and act like a member of the enclosing class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. SACHIN KHARADE

  6. class Outer_Demo { int num; //inner class private class Inner_Demo { public void print() { } } //Accessing the inner class from the method within the outer class void display_Inner() { Inner_Demo inner = new Inner_Demo(); inner.print(); } } System.out.println("This is an inner class"); SACHIN KHARADE

  7. public class My_class { public static void main(String args[]) { //Instantiating the outer class Outer_Demo outer=new Outer_Demo(); //Accessing the display_Inner() method. } } outer.display_Inner(); SACHIN KHARADE

  8. Inner classes are of three types depending on how and where you define them. They are: Inner Class Method-local Inner Classes Anonymous Inner Class SACHIN KHARADE

  9. Method-local Inner Class When an inner class is defined inside the method of Outer Class it becomes Method local inner class. Method local inner class can be instantiated within the method where it is defined and no where else. Method local inner class can only be declared as final or abstract. Method local class can only access global variables or method local variables if declared as final. SACHIN KHARADE

  10. public class Outerclass { //instance method of the outer class void my_Method() { int num=23; //method-local inner class class MethodInner_Demo { }//end of inner class public void print() { System.out.println("This is method inner class "+num); } } } public static void main(String args[]) { Outerclass outer =new Outerclass(); outer.my_Method(); } //Accessing the inner class MethodInner_Demo inner=new MethodInner_Demo(); inner.print(); SACHIN KHARADE

  11. Output will be: SACHIN KHARADE

  12. Advantage of java inner classes Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private and still have its own type. The outer class members which are going to be used by the inner class can be made private and the inner class members can be hidden from the classes in the same package. This increases the level of encapsulation. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only. Code Optimization: It requires less code to write. SACHIN KHARADE

More Related Content