Description :-
A class with no name is called Anonymous inner class.
----------------------------------------------------------------------------------------------------------------
Key Points :-
1. Normally used when you want to add small functionality and
don’t want to create a new class for it. Look for some real-time examples below
for clarity.
2. Increases readability of the code. ( Can be argued because
of weird syntax)
3. Increases performance as you are not adding extra classes
for small functionality. (Check References section)
4. Anonymous class means creating a sub-class with no name and
override the functionality.
5. Anonymous class can be created for a class, abstract class
and interface.
6. We can think about polymorphism as anonymous inner class is
a subclass object and the reference variable can be thought of as super class
reference.
7. Weird syntax
Hello hello = new Hello() {
// override method
public String sayHello(){
return "Anonymous Hello";
}
}; // end of class
8. When you want to add actionListener for button.
Example :-
JButton jButton = new JButton(“Click”);
jButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event){
// code
}
});
9. When you want to add customized sorting for the list.
Example :-
Collections.sort(list, new Comparator<Employee>(){
public int
compare(Employee emp1, Employee emp2){
// code
}
});
10. When you want to create a thread from Runnable interface.
Example :-
Thread thread = new Thread( new Runnable(){
public void run(){
// code
// code
}
});
----------------------------------------------------------------------------------------------------------------
Program :-
Normal class
package com.thebeautifuljava.anonymous;
public class Hello {
public String sayHello() {
return "hello";
}
}
----------------------------------------------------------------------------------------------------------------
Abstract class
package com.thebeautifuljava.anonymous;
public abstract class AbstractHello {
public abstract String sayHello();
}
----------------------------------------------------------------------------------------------------------------
Interface
package com.thebeautifuljava.anonymous;
public interface InterfaceHello {
public abstract String sayHello();
}
----------------------------------------------------------------------------------------------------------------
main() shows how anonymous class overrides method for each of the above
package com.thebeautifuljava.anonymous;
public class AnonymousDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// override method of normal class
Hello hello = new Hello() {
public String sayHello() {
// TODO Auto-generated method stub
return "Anonymous Hello";
}
};
System.out.println(hello.sayHello());
AbstractHello abstractHello = new AbstractHello() {
// override method of abstract class
public String sayHello() {
// TODO Auto-generated method stub
return "Abstract Anonymous Hello";
}
};
System.out.println(abstractHello.sayHello());
InterfaceHello interfaceHello = new InterfaceHello() {
// override method of interface
public String sayHello() {
// TODO Auto-generated method stub
return "Interface Anonymous Hello";
}
};
System.out.println(interfaceHello.sayHello());
}
}
----------------------------------------------------------------------------------------------------------------Override method which has parameter
package com.thebeautifuljava.anonymous;
public class HelloParameter {
public String saySomething(String string) {
return string;
}
}
package com.thebeautifuljava.anonymous;
public class AnonymousParameterDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// with anonymous class
HelloParameter helloParameter = new HelloParameter() {
public String saySomething(String string) {
return string + " Anonymous";
}
};
System.out.println(helloParameter.saySomething("Hi"));
// without anonymous class
HelloParameter helloParameterNormal = new HelloParameter();
System.out.println(helloParameterNormal.saySomething("Hi"));
}
}
----------------------------------------------------------------------------------------------------------------
References :-

No comments:
Post a Comment