Classes/Objects  «Prev 


Anonymous Inner Class

For example, consider a situation where you need to create an instance of an object without creating a subclass of a class and also perform additional tasks such as method overriding.
public class App {
 public static void main(String[] args) {
  Thread t1 = new Thread(new Runnable(){
    public void run() {
     for( int i = 0; i < 10 ; i++){
      System.out.println("Hello " + i );
      try {
       Thread.sleep(100);
      } catch (InterruptedException e) {
         e.printStackTrace();
     }
    }
   }
  }); t1.start();
 }
}

In the above code, an anonymous inner class is created for the Thread constructor. As the name implies, an anonymous inner class does not have a name.
The declaration of the class automatically derives from the instance-creation expression.
They are also referred to simply as anonymous classes. An anonymous class is useful in almost all situations where you can use local inner classes. A local inner class has a name, whereas an anonymous inner class does not. An additional difference is that an anonymous inner class cannot have any explicit constructors. A constructor is named after the name of the class, and since an anonymous class has no name, it follows that you cannot define a constructor.
anonymous interfaces do not exist.
Here is an example just to address the syntax of a local class:


class TestClass {
  void someFunction() {
    new Object() { };
  }
}

In the statement
new Object() { };
, you are declaring a derived class of Object directly using the new keyword.
It does not define any code and returns an instance of that derived object. The created object is not used anywhere, so it is ignored. The new expression invokes the default constructor here; you could choose to invoke a multiple argument constructor of the base class by passing arguments in the new expression. When defining an anonymous class, it implicitly extends the base class (which is the Object base class here).

Points to Remember

These points about anonymous classes concern questions that might be asked on the OPCJP 7 exam:
  1. Anonymous classes are defined in the new expression itself, so you cannot create multiple objects of an anonymous class.
  2. You cannot explicitly extend a class or explicitly implement interfaces when defining an anonymous class.

Anonymouns Inner Interface

In the following example, an interface Inter is declared that contains 2 methods.
An anonymous interface is created that implements the two methods.

package com.java.aic;
interface Inter{
  public String getString();
  public int getNumber();
}
// -----
public class InnerInterface { 
  Inter instance = new Inter() {  
    public String getString(){ 
      return "Hello"; 
    } 
    public int getNumber(){
      return 3;   
    }		
  };
  public static void main(String args[]){
    InnerInterface in1 = new InnerInterface();
    String s1 = in1.instance.getString();
    System.out.println("s1= " + s1);
    int i1 = in1.instance.getNumber();
    System.out.println("i1= " + i1);
  }
}
/* 
Program Output 
s1= Hello
i1= 3
*/