Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What is the use of anonymous inner classes ?

Answers were Sorted based on User's Feedback



What is the use of anonymous inner classes ?..

Answer / ram

You need object of class 'X', but you want some custom
behavior for the object, not following the behavior defined
by the class 'X'.
Solution 1: create subclass 'Y' extending 'X'. Override
required methods(for custom behavior). create instance of Y
in your code and use it.
Solution 2: But you can achieve the above thing (need a
instance with different behavior) using anonymous inner
classes. The advantage here is, you don't even need to
create a subclass. Create an Anonymous inner class on the
fly, Just override the required behavior, and create an
instance and use it. Here is an example :

I have a class 'A'. By default, its getValue() method
returns whatever the value x holds. But i want to override
this behavior, it should return x+15.

public class AnonymousInnerClass {

public static void main(String[] args) {
A a = new A(10) {
@Override
public int getValue() {
return x + 15;
}
};
System.out.println(a.getValue());// 25 and not 10
}

}

class A {
int x;

public int getValue() {
return x;
}

A(int x) {
this.x = x;
}
}

Is This Answer Correct ?    0 Yes 0 No

What is the use of anonymous inner classes ?..

Answer / jubin thomas, bhilai(mpccet)

Anonymous class is a class which has no methods defined in
it.
Eg.
class a
{
public void hello();
}
class b
{
a obj = new a();
public void hello()
{
System.out.println("Hello World");
}
}

Is This Answer Correct ?    2 Yes 64 No

Post New Answer

More Core Java Interview Questions

What is meant by object?

0 Answers  


What is passing value java?

0 Answers  


Hi, well i am unable to understand that why it is mandatory to have same hashcode, if two objects are same? Thanks in advance.

5 Answers  


What is string immutability?

0 Answers  


What are init(), start() methods and whey they are called?

2 Answers  


what r advatages of websphere? & how to deploy?

0 Answers   Saksoft,


What is the purpose of default constructor?

0 Answers  


What is the purpose of setAutoCommit() ?

1 Answers   Google,


Can subclass overriding method declare an exception if parent class method doesn't throw an exception?

0 Answers  


What is the original name of java?

0 Answers  


What is java’s garbage collected heap?

0 Answers  


What is a byte array?

0 Answers  


Categories