Java Programming - Inner Classes - Discussion

Discussion Forum : Inner Classes - General Questions (Q.No. 1)
1.
Which is true about an anonymous inner class?
It can extend exactly one class and implement exactly one interface.
It can extend exactly one class and can implement multiple interfaces.
It can extend exactly one class or implement exactly one interface.
It can implement multiple interfaces regardless of whether it also extends a class.
Answer: Option
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.

Discussion:
11 comments Page 1 of 2.

Anonymous said:   9 months ago
Here option A&C are same. Please explain the answer.

B.Chandrakala said:   3 years ago
Agree, Option C is the right answer.

Sangeetha R said:   7 years ago
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.

Abhi said:   9 years ago
I can't get the answer. Please clarify me.

Amaziane said:   1 decade ago
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).

Raja said:   1 decade ago
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.

Abdul Barik said:   1 decade ago
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();
}
}

Moin khan said:   1 decade ago
What is anonymous class please explain in detail?

Zishan said:   1 decade ago
I don't understand exact meaning. Please explain in detail with example.

Ravi Shankar said:   1 decade ago
Yes in child class you can implement any number of interface but in inner class only implement one interface or can extend one interface.


Post your comments here:

Your comments will be displayed after verification.