Java Programming - Inner Classes - Discussion

Discussion :: Inner Classes - General Questions (Q.No.1)

1. 

Which is true about an anonymous inner class?

[A]. It can extend exactly one class and implement exactly one interface.
[B]. It can extend exactly one class and can implement multiple interfaces.
[C]. It can extend exactly one class or implement exactly one interface.
[D]. It can implement multiple interfaces regardless of whether it also extends a class.

Answer: Option C

Explanation:

Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous class extends that one class).

Option A, B, D, and E are all incorrect because they don't follow the syntax rules described in the response for answer Option C.


Arkadeep said: (Jun 16, 2011)  
Please give details about inner classes. Are they different from child classes?

Ravi Shankar said: (Aug 12, 2011)  
Yes in child class you can implement any number of interface but in inner class only implement one interface or can extend one interface.

Zishan said: (Sep 21, 2011)  
I don't understand exact meaning. Please explain in detail with example.

Moin Khan said: (Sep 13, 2013)  
What is anonymous class please explain in detail?

Abdul Barik said: (Jan 1, 2014)  
1 - A class which has no name is called anonymous class.

2 - We can use this class one time at object creation time because we do not store the reference id of this class.

3 - Anonymous class can be created by:

* Class (may be abstract class also).
* Interface

Like:

abstract class Fruit{
abstract void eat();
}

class Test{
public static void main(String args[]){
Fruit f=new Fruit(){
void eat(){System.out.println("nice fruits");}
};

p.eat();
}
}

Raja said: (Apr 21, 2014)  
I don't understand, how come "It can extend exactly one class or implement exactly one interface" is right answer?

I think we CANNOT explicitly extend a class or implement interface when defining an ANONYMOUS INNER CLASS.

Amaziane said: (Jul 1, 2014)  
I want just to correct one error on intervention of Ravi Shankar said: (Fri, Aug 12, 2011 12:54:23 AM).

Yes in child class you can implement any number of interface but in inner class only implement one interface or can extend one class (instead of one interface).

Abhi said: (Sep 7, 2016)  
I can't get the answer. Please clarify me.

Sangeetha R said: (Oct 3, 2018)  
Difference between Normal/Regular class and Anonymous Inner class:

A normal class can implement any number of interfaces but anonymous inner class can implement only one interface at a time.

A regular class can extend a class and implement any number of interface simultaneously. But anonymous Inner class can extend a class or can implement an interface but not both at a time.

For regular/normal class, we can write any number of constructors but we can't write any constructor for anonymous Inner class because anonymous class does not have any name and while defining constructor class name and constructor name must be same.

B.Chandrakala said: (May 17, 2022)  
Agree, Option C is the right answer.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.