Anonymous Inner Class It is an inner class without a name and for which only a

Anonymous inner class it is an inner class without a

This preview shows page 182 - 186 out of 620 pages.

Anonymous Inner Class It is an inner class without a name and for which only a single object is created. Anonymous inner classes are very useful in writing implementation classes for listener interfaces in graphics programming. To understand the use of anonymous inner class, let us take a program, where we display a push button, which \\rill terminate the application when clicked. For this purpose, we should follow the steps~ t D Add ActionListener interface to the push button 'b'. This is done by addActionListener () method. But this method expects an object of ActionListener interface. Since it is not possible to create an object to an interface, we create an object to the implementation class 0 the interface. . J:). am Act; on s ener new M class (1- . Here, Myclass is assumed to be the iJIlplementation class of the ActionListener interface. D Now we write the Myclass as: Since, Myclass is an implementation class of ActionListener interface, the metho actionPerformed () should be implemented in this class. This method code is executed when th push button is clicked by the user. Program 4: Write a program to create a push button and add it to a frame. But: () { ! !creare a Button b =
Image of page 182
Relationship Between Objects 179 OK :;:rreceding program, we add ActionListener interface to provide some action to the button. Here, istener needs an implementation class. We write Myclass as the implementation class of - nListener interface. done in Program 4, we need not write a separate Myclass and pass its object to ~~ctionListe)1er () method, as: i () o of gIl .er cs ~ '1e d, we can directly copy the code of Myclass into this method parameter, as shown here:
Image of page 183
~ Chapter 14 Please do not think that we are creating an object to ActionListener in the preceding methocL are, in fact, creating an object of Myc1ass and copying the ent'rre Class colle llUl ~nt TI' parameter. This is possible only with anonymous inner class. Here, we did not write the name 0 class in the method parameter: Such a class is called 'anonymous inner class'so, we call 'Myc as an anonymous inner class. Let us understand the following points from the preceding discussion: o Myclass is inner class inside But class. o The name 'Myclass' is not written in its outer class, Le., But class .. o Myclass object.is created only once .. o . Myclass code is directly copied into the method parameter. i lexit the apphcation system.exit(D); Please see Program 5, which is the anonymous inner class version of the Program 4. Program 5: Write a program by taking Myclass as an anonymous inner class whose name . mentioned in the But class's addActionListener () method. What is anonymous inner class? It is an inner class whose name is not written in the outer class and for which only one object created.
Image of page 184
t ok I I c: > c:-'> Relationship Between Objects 181 77set toe size or frame to w;otn: 400 px: aDo oeignt: 300 px obj.setSize(400,30fr); //d;splay th~ frame obj.setV;sible(true); t: } elusion -0 relate the objects of two classes, we declare the reference of one class as an instance variable in another class. This is useful to access the members of that class whose object is
Image of page 185
Image of page 186

You've reached the end of your free preview.

Want to read all 620 pages?

  • Fall '08
  • STAFF

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture