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.
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-
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
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
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.
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.
0 comments:
Post a Comment