• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

interface vs inner classes

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the statements are true .
as a result the following code works
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
easy to remember: because it is not possible to create an instance of an interface, nested classes within an interface are static.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic