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
Explain public static void main(string args[]) in java.
What does || mean in code?
Is 9 a prime number?
What is JDBC Driver interface?How can you retrieve data from the ResultSet
What is difference between add() and addelement() in vector?
What is the access scope of protected access specifier?
What are the 5 types of research methods?
how to create multithreaded program? Explain different ways of using thread? : Java thread
What is identifier give example?
What is continuity of a function?
Can a class be private?
What is the main purpose of java?
Can you start a thread twice in Java?
How do you convert an int to a string in java?
How to instantiate member inner class?