1 / 12

Anonymous Classes

Anonymous Classes.

Download Presentation

Anonymous Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Anonymous Classes • A local class is a class that is defined in a block. This could be a method body, a constructor, a local block, a static initializer or an instance intializer. Such a local class is only visible within the context of the block. I.e. the name of the class is only valid in the context of the block in which it is defined. A local class cannot be specified with the keyword static. However, if the context is statc(i.e. a static method or a static initializer) then the local class is implicitly static. Otherwise, the local class is non-static • Like non-static inner classes, an instance of a non-static class is passed a hidden reference designating an instance of its enclosing class in its constructors, and this gives non-static local classes much of the same capability as non-static inner classes • A local class can access members defined within the class. • A local class can access final local variables, final method parameters . Some restrictions which apply to local classes are: • 1- Local classes cannot have static members, as the cannot provide class-specific services. • 2- Local classes cannot have any accessibility. This restriction applies to local variables, and is also enforced for local classes Anonymous Classes

  2. Local Classes Example (Illustrates Access Rules of Local Classes) Anonymous Classes

  3. Local Class Example static void staticMethod(final int i) { // Static Method final int j = 10; int k; class StaticLocalE extends SuperB { // Static local class // static double d; // (11) Not OK. Only non-static members allowed. int ii = i; // (12) final from enclosing method. int jj = j; // (13) final from enclosing method. // double kk = k; // (14) Not OK. Non-final from enclosing method. // double zz = z; // (15) Not OK. Non-static member. int pp = p; // (16) static from enclosing class. // double yy = y; // (17) Not OK. Non-static member. int mm = m; // (18) static from enclosing class. double xx = x; // (19) non-static inherited from superclass int nn = n; // (20) static from superclass } } } Anonymous Classes

  4. Local Classes • A local class can access final local variables, final method parameters an final catch-block parameters in the socpe of the local context. Such final variables are also read-only in the local class. This situation is shown at(2)and (3), where the final paramter I and the final local variable j of the method nonStaticMethod() in the non-static local class NonStaticLocalID are accessed. This also applies to static local classes, as shown at(12) and (13. Access to non-final local variables is not permitted from local classes, as shown at(4) and(14). • A non-static local class can access memebers defined in the enclosing class. This situation is shown at(5) and(6), where the instance variable z and static variable p defined in the enclosing class TopLevelA are accessed, respectively. The special form of the this construct can be used for explicit refeencing of the members defined in the enclosing class: double zz=TopLevelA.this.z; • Howerver, aa static local class cn only directly acces static members defined in the enclosing class, as shown at(16), but non-static members, as shown at (15) Anonymous Classes

  5. Anonymous Classes • Classes are usually first defined and then instantiated using the new operator. Anonymous classes combine the process of definition and instantiation into a single step. Anonymous classes are defined at the location they are instantiated, using additional syntax with the new operator. As these do not have a name, an instance of the class can only be created together with the definition • Anonymous class is the act of declaring an inner class without naming it • An anonymous class is declared as part of a new expression and must either be a subclass or implement an interface new <className>(< argumentList> ) {< classBody>} new interfaceName( ) { classBody} classNameis the name of the superclass of the anonymous class, while interfaceName is the name of the interface to which the anonymous class must conform. The argumentList is a parameter list that is used to call a matching constructor of the named superclass. Anonymous class cannot define constructors (as it does not have name). No extends clause is used in the construct Anonymous Classes

  6. Anonymous Classes (Cont’d) • Class is anonymous if it does not have a name, In a program, something that is only used once doesn’t usually need a name. For example, you can replace Rectangle rect =new Rectangle(100,100,100,100); g2.fill(rect); With g2.fill(new Rectangle (100,100,100,100)); • If the rectangle is not used elsewhere in the same method. The object new Rectangle(100,100,100,100)is an anonymous object. Anonymous Classes

  7. Anonymous Inner Classes Look at the code shown below: public class Parcel { public Contents cont() { return new Contents() { private int i = 11; public int value() { return i; } // End of method value() }; // Semicolon required in this case } // End of method cont() public static void main(String[] args) { Parcel p = new Parcel(); Contents c = p.cont(); } // End of method main() } // End of class Parcel Anonymous Inner Class This syntax means: ”create an object of an anonymous class that’s inherited from Contents”. The reference returned by the new expression is automatically upcast to contents reference. The method cont() of the class Parcel returns a reference to an anonymous class defined within the same method and called Contents. Anonymous Classes

  8. Anonymous Class Example Anonymous Classes

  9. Anonymous Class Example (Cont’d) public class Anon { public static void main(String args[]) { // (9) IDrawable[] drawables = { // (10) new Painter().createShape(), // (11) non-static anonymous class Painter.createIDrawable(), // (12) static anonymous class new Painter().createIDrawable() // (13) static anonymous class}; for (int i = 0; i < drawables.length; i++) // (14) drawables[i].draw(); System.out.println("Anonymous Class Names:"); System.out.println(drawables[0].getClass()); // (15) System.out.println(drawables[1].getClass()); // (16) }} Anonymous Classes

  10. Anonymous Classes The instance method createShape() at (4) deifines a non-static anonymous class at(5), which extends the superclass Shape. The anonymous class overrides the inherited method draw(). As references to an anonymous class cannot be declared, the functionality of the class is only available through superclass references, Usually it makes sense to either override methods from the superclass or implement abstract methods from the superclass. An anonymous class implementing an interface , the static method createIDrawable() at (7) deifnes a static anonymous class at(8), which implements the interface IDrawable by providing an implementation of the method draw(. The class Client creates on instance at (11) of the non-static anonymous class defined at(5), and two instances at(12) and (13) respectively of the static anonymous class defined at(8). The program output shows the polymorphic behavior and the runtime types of the objects. Anonymous Classes

  11. Anonymous Class Example 2 Anonymous Classes

  12. Anonymous Class Example 2 (Cont’d) public class Myapplet extends Applet { public MyApplet() { ......... addMouseListener(new MouseClickListener()); } private class MouseClickListener extends MouseAdapter { public void mouseClicked(MouseEvent event) { // mouse click action goes here } }}public class Myapplet extends Applet { public MyApplet() { ......... addMouseListener(new MouseClickListener() { // name of superclass public void mouseClicked(MouseEvent event) { // methods of anonymous subclass // mouse click action goes here } });}.......... } Anonymous Classes

More Related