Like other classes in java , anonymous is also a type of class but it doesn't have any name like other classes have their own name either defined by the user or pre-defined in the java by the java vendors. So anonymous class is defined as the class without name and it is defined by the user only.
No need of wonder that how a class can exist without any name ?
Its not that it doesn't have any name but generated by the compiler at the run-time. So user cannot guess what compiler will generate the name for an anonymous class. Due of this reason we cannot be define any constructor for the anonymous class. And this is the answer to the answer to the question why we cannot create an constructor of an anonymous class?

An anonymous will be defined dynamically by the { } after the constructor of the current class. Like this,
 new < constructor > { } ;

For every anonymous class a .class file will be generated like this,
< outer_class > $ < int_value > . class

< int_value > --> represents the number of times we are creating the anonymous class for the same outer class.

Anonymous class is always be the sub-class of the class/interface whose name is used while creating the anonymous class.  For ex-

public class prog{

public static void main(String[] args) {

 prog p = new prog(){};

 prog p1 = new prog(){}; 

     }
}

In the above program both the statement is going to create an anonymous class which is the sub-class of class prog. If you need any conformation for the point of sub-class then de-compile the .class generated for the anonymous class where you will find that

class prog$1 extends prog{ } --> For p object

class prog$2 extends prog{ } ---> For p1 object .

So from the above decompiled code we can confirm that anonymous class will act as the sub-class of the class through which it is created.

Important point regarding Anonymous class

1. We cannot create any constructor or reference variable for the anonymous class as we need to know class name for creating a constructor.

2. As it the sub-class of its super type so we can store its object reference to its super class reference.

3. We cannot create multiple object for the an anonymous class as because user doesn't know the name of the anonymous class.

4. Whenever we try to create another object with the new operator the compiler will create another class extending its super type.

5. If we are storing the sub-class reference to its super type reference then we can be able to access the super class inherited members.

6. If we are not storing the anonymous class object reference to its reference then we can be able to access only one member. for ex-

public class prog{

public static void main(String[] args) { 

 new prog(){

 void display() {

        System.out.println("Anonymous class method");
    } 

 }.display();
     }
}  

We can see that we can be able to access display method of the anonymous class, if other methods are defined then we cannot access if we are not storing the object reference to its super type.

7. To access the other member defined in the anonymous class which is the overridden method of its super type so we need to write the method signature to the super type then only we can access the method defined in the anonymous class with the super type object reference storing the anonymous class object reference. For ex-

public class prog{

void show(){}
void display(){}

public static void main(String[] args) {

 prog p = new pro(){

     void display(){
        System.out.println("Anonymous class method");
     }

     void show(){
        System.out.println("By Object Reference");
     }
 };

 p.show();
 p.display();

     }
}

So whenever there is the need of invoking multiple method we need to define the anonymous class method as the overridden method as because we cannot directly call two method with the object of the anonymous class. And we know that we can be able access single method at a time with the single object.  

Related Posts:

  • Importance of " import " statement in java Import statement in java is used in java to import all the predefined classes defined in a package and within that classes many methods are defined. Package is just like the directory in the file system.  Whenever we a… Read More
  • Compilation and Interpretation in java In this post i will tell you something about compilation which is a task of compiler and on other side interpretation which is the task of interpreter.  There are few queries related to this topic which i am going … Read More
  • Literals in java Literals in java define the actual value we are using for variables, constants or to perform any operation. There are seven literals in java in which one literals is introduced from java 7 i.e binary literals. They are a… Read More
  • Program to print a simple text in java After installing java and setting the path for it now its turn to have some fun with codes. Now i am going to tell you how to print your text in java. Its a very simple program and basically for beginners. There are some im… Read More
  • How and Why to use main() method in java? In this post i will tell you something interesting about main() method in java. Like What is the use of main() method? How we are going to use main() method? Why we are using main() method in our java program? You will find… Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic