explain Anonynous inner class?

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


Please Help Members By Posting Answers For Below Questions

What is the difference between logical data independence and physical data independence?

525


What is "this" keyword in java? Explain

652


Which collection is thread safe in java?

501


what is the messsage u r going to get from an objectoriented programing?

1601


What is a generic type?

552






Can we restart a thread already started in java?

576


How do you detect memory leaks?

575


Is there any sort function in java?

571


Can a private method be declared as static?

571


Is it possible to cast an int value into a byte variable? What would happen if the value of int is larger than byte?

539


What are alternatives to java serialization?

594


Explain the different forms of polymorphism?

559


What is the size of int in 64-bit jvm?

546


What is another word for methodology?

514


How to perform selection sort in java?

573