aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface vs inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "interface vs inner classes" Watch "interface vs inner classes" New topic
Author

interface vs inner classes

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
Which of the following statements is true?
a) An interface can contain a nested top-level inner class.
b) An interface can contain a member inner class.
c) A member inner class can implement an interface.
d) A static method can contain a local class.
e) A static method can contain a nested top-level class.
i have written a small program to illustrate above, all the conditions are true according to me, pls help....


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
All the statements are true .
as a result the following code works
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by srinivas bolloju:
Which of the following statements is true?
a) An interface can contain a nested top-level inner class.
b) An interface can contain a member inner class.
c) A member inner class can implement an interface.
d) A static method can contain a local class.
e) A static method can contain a nested top-level class.
i have written a small program to illustrate above, all the conditions are true according to me, pls help....

srinivas,
Based on your code example your conclusions are logical, but are not correct. You have assumed that class A is non-static because you did not provide a static modifier for the declaration of class A. However, all member type declarations within an interface a implicitly public and static. It is not possible to declare a non-static class within an interface.
The following code example demonstrates that a top-level nested class can be instantiated without first creating an instance of the enclosing class. However, a non-static member class can not be instantiated without first instantiating an instance of the enclosing class.

D.E is a static nested class and therefore may be instantiated without first creating an instance of class D. However, non-static member class F can not be instantiated without first creating an instance of D.
In your code example, an instance of nested class A can be instantiated without first creating an instance of interface intera. Of course, that must be the case because it is not possible to create an instance of an interface. Therefore, nested class A must be implicitly static.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I must slightly modify my previous statement. Rather than say the following.

It is not possible to declare a non-static class within an interface.

I should qualify that statement by adding the adjective "member" to the word "class".

It is not possible to declare a non-static member class within an interface.

The above modification is necessary because a local class may be declared within a code block contained within an enclosing interface, and local classes are not static.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
easy to remember: because it is not possible to create an instance of an interface, nested classes within an interface are static.


SCJP2. Please Indent your code using UBB Code
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
e) A static method can contain a nested top-level class.

A method (static or otherwise) can contain only a local class or an anonymous class. By definition such a class is not "top-level".


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: interface vs inner classes
 
Similar Threads
doubt - innerclasses & interface
Nested class/interface doubt
innerclass question
Inner class concept
classes(inner, nested)