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
Is jdk required on each machine to run a java program?
Explain about class in java?
What is data structure in java?
Is boolean a data type in java?
How do you detect memory leaks?
What is the full name of java?
how is final different from finally and finalize in java?
What is the difference between the reader/writer class hierarchy and the inputstream/outputstream class hierarchy in java programming?
What is console based application in java?
How many bits is a string?
Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.
Explain the selection sort algorithm and state its time complexity?
Explain about the dynamic behavior of core java?
How do you add spaces in java?
Can private class be inherited in java?