Wednesday 12 February 2014

Anonymous Classes

Anonymous classes are just like local classes, except these don't have name.

Syntax of Anonymous Classes
The syntax of an anonymous class expression is like the invocation of a constructor, except there is a class definition contained in a block of code.

Example   
Uniform zpschool = new Uniform(){
 String color;
 void setColor(String color){
  this.color = color;
 }

 String getColor(){
  return color;
 }
}; 


The anonymous class expression consists of the following:
       The new operator
     The name of an interface to implement or a class to extend.
     Parentheses that contain the arguments to a constructor

Creating Anonymous Classes using Abstract class
class AnonymousEx{
 abstract class Uniform{
  abstract void setColor(String color);
  abstract String getColor();
 }

 Uniform zpschool = new Uniform(){
  String color;
  
  void setColor(String color){
   this.color = color;
  }
  
  String getColor(){
   return color;
  }
 };

 public static void main(String args[]){
  AnonymousEx obj = new AnonymousEx();
  obj.zpschool.setColor("Blue");
  System.out.println(obj.zpschool.getColor());
 }
}

Output
Blue
 

Creating Anonymous class using Interface
class AnonymousEx{
 interface Uniform{
  void setColor(String color);
  String getColor();
 }

 Uniform zpschool = new Uniform(){
  String color;
  
  public void setColor(String color){
   this.color = color;
  }

  public String getColor(){
   return color;
  }
 };

 public static void main(String args[]){
  AnonymousEx obj = new AnonymousEx();
  obj.zpschool.setColor("Blue");
  System.out.println(obj.zpschool.getColor());
 }
}

Output
Blue
1. Can I declare an interface inside Class ?
Yes

2. Can I declare abstract class inside class ?
Yes

3. Can I create an object for an Interface ?
Yes, Using Anonymous classes

4. Can I create an object for abstract class ?
Yes, Using Anonymous classes

5. An anonymous class cannot access local variables in its enclosing scope that are not declared as final
class AnonymousEx{
 String name="HI";

 interface Uniform{
  void setColor(String color);
   String getColor();
 }

 public void print(){
  int val = 10;
   
  class myClass implements Uniform{
   String color;

   public void setColor(String s){
    color = s;
   }

   public String getColor(){
    System.out.println(val);
    return color;
   }
  }
 }
}

When you try to compile compiler throws the below error
        
AnonymousEx.java:18: error: local variable val is accessed from within inner class; needs to be          declared final
System.out.println(val);
^
1 error

To make the program compiles fine, define the local variable as final
     final int val = 10;
  
6. You cannot declare static initializers or member interfaces in an anonymous class.
class AnonymousEx{
 String name="HI";
 
 interface Uniform{
  void setColor(String color);
  String getColor();
 }

 public void print(){
  class myClass implements Uniform{
   static String color;
   public void setColor(String s){
    color = s;
   }

   public String getColor(){
    return color;
   }
  }
 }
}


When you tries to compile the above program, compiler throws the below error

AnonymousEx.java:12: error: Illegal static declaration in inner class myClass
static String color;
^
modifier 'static' is only allowed in constant variable declarations
1 error

7. An anonymous class can have static members provided that they are constant variables

8. Can I declare extra methods in Anonymous classes ?
Yes. It is valid

Example
class AnonymousEx{
 interface Uniform{
  void setColor(String color);
  String getColor();
 }

 Uniform zpschool = new Uniform(){
  String color;

  public void setColor(String color){
   this.color = color;
  }

  public String getColor(){
   return color;
  } 

  void print(){
   System.out.println("I am Extra method");
  }
 };
}
 
As you see in the above program, interface Uniform has 2 abstract methods, setColor, getColor.But the Anonymous class has extra method print().


9. Is below program compile ?
class AnonymousEx{
 abstract class Uniform{
  void setColor(String color);
  String getColor();
   }
}
No, Compiler throws the below error.
AnonymousEx.java:4: error: missing method body, or declare abstract
void setColor(String color);
^
AnonymousEx.java:5: error: missing method body, or declare abstract
String getColor();
^
2 errors

To solve the above error, make the methods in the abstract class as abstract

class AnonymousEx{
 abstract class Uniform{
  abstract void setColor(String color);
  abstract String getColor();
 }
}
         

Local Classes                                                 Lambda Expressions                                                 Home

No comments:

Post a Comment