adapter class - chavanpragatip.files.wordpress.com · anonymous inner class • it is an inner...

16
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. Department of Computer Engineering AJP Unit III Mrs. Chavan P.P. 1

Upload: others

Post on 19-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

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. Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 1

Page 2: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

• 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.

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 2

Page 3: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

Listener Interface Adapter Class

ComponentListener ComponentAdapter

ContainerListener ContainerAdapter

FocusListener FocusAdapter

KeyListener KeyAdapter

MouseListener MouseAdapter

MouseMotionListener MouseMotionAdapter

WindowListener WindowAdapter

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 3

Page 4: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

import java.awt.*;

import java.awt.event.*;

public class Mouse extends MouseAdapter

{

public void mouseClicked(MouseEvent evt)

{

System.out.println(“Mouse Clicked”);

}

}

Without extending the 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)

{

System.out.println(“Mouse Clicked”);

}

public void mousePressed(MouseEvent evt) {}

public void mouseReleased(MouseEvent evt) {}

public void mouseEntered(MouseEvent evt) {}

public void mouseExited(MouseEvent evt) {}

} Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 4

Page 5: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

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.

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 5

Page 6: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

class Outer_Demo

{

int num;

//inner class

private class Inner_Demo

{

public void print()

{

System.out.println("This is an inner class");

}

}

//Accessing the inner class from the method within the outer class

void display_Inner()

{

Inner_Demo inner = new Inner_Demo();

inner.print();

}

} Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 6

Page 7: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

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(); } }

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 7

Page 8: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

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

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 8

Page 9: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

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.

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 9

Page 10: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

public class Outerclass

{

void my_Method()

{

int num=23;

//method-local inner class

class MethodInner_Demo

{

public void print()

{

System.out.println("This is method inner class "+num);

}

}//end of inner class

//Accessing the inner class

MethodInner_Demo inner=new MethodInner_Demo();

inner.print();

}

public static void main(String args[])

{

Outerclass outer =new Outerclass();

outer.my_Method();

}

} Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 10

Page 11: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

Output will be:

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 11

Page 12: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

Anonymous Inner Class

• It is an inner class without a name and for which only a single object is created.

• An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class.

• Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 12

Page 13: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

interface Age { int x = 21; void getAge(); } class MyClass implements Age { public void getAge() { System.out.print("Age is "+x); } } class AnonymousDemo { public static void main(String[] args) { MyClass obj=new MyClass(); obj.getAge(); } }

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 13

Page 14: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

interface Age { int x = 21; void getAge(); } class AnonymousDemo { public static void main(String[] args) { Age oj1 = new Age() { public void getAge() { // printing age System.out.print("Age is "+x); } }; oj1.getAge(); } }

Example Anonymous Class that implements an interface

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 14

Page 15: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

class MyThread { public static void main(String[] args) { Thread t = new Thread() { public void run() { System.out.println("Child Thread"); } }; t.start(); System.out.println("Main Thread"); } }

Example Anonymous Class that extends a class

Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 15

Page 16: Adapter Class - chavanpragatip.files.wordpress.com · Anonymous Inner Class • It is an inner class without a name and for which only a single object is created. • An anonymous

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. Department of Computer Engineering

AJP Unit III Mrs. Chavan P.P. 16