Answer Posted / shaik baji
Anonymous inner class comes in two forms
1)Normal Anonymous Inner Class
2)Parametrized Anonymous Inner Class
1) Normal Anonymous Inner Class:
Again the Normal Anonymous Inner class is two types
a)Extending the class by Anonymous Inner Class
b)Implementing the interface by Anonymous Inner
Class
a)Extending the class by Anonymous Inner Class
class One
{
void printOne()
{
System.out.println("One");
}
}
class AnonymousDemoByClass
{
public static void main(String Arg[])
{
One obj = new One(){
void printOne()
{
printTwo();
}
void printTwo()
{
System.out.println
("Two");
}
};
obj.printOne();
}
}
Output: Two
b) Implementing the interface by Anonymous Inner
Class
interface One
{
void printOne();
}
class AnonymousDemoByInterface
{
public static void main(String Arg[])
{
One obj = new One(){
public void printOne()
{
printTwo();
}
void printTwo()
{
System.out.println
("Two");
}
};
obj.printOne();
}
}
Output: Two
2)Parametrized Anonymous Inner Class:
Here we are implementing our Anonymous inner class as a
paramer to any method.
interface One
{
void printOne();
}
class ParameterizedAnonymousDemo
{
public static void main(String Arg[])
{
ParameterizedAnonymousDemo obj =
new ParameterizedAnonymousDemo();
obj.doSomething(new One(){
public void printOne
()
{
System.out.println("One");
}
});
}
public void doSomething(One objOne)
{
objOne.printOne();
}
}
Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
What is the difference between logical data independence and physical data independence?
What is "this" keyword in java? Explain
Which collection is thread safe in java?
what is the messsage u r going to get from an objectoriented programing?
What is a generic type?
Can we restart a thread already started in java?
How do you detect memory leaks?
Is there any sort function in java?
Can a private method be declared as static?
Is it possible to cast an int value into a byte variable? What would happen if the value of int is larger than byte?
What are alternatives to java serialization?
Explain the different forms of polymorphism?
What is the size of int in 64-bit jvm?
What is another word for methodology?
How to perform selection sort in java?