• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Interface doubt

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing interface?
a. abstract
b. implements
c. final
d. private
e. protected
f. public

Answer: a,f


Source of the question


Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public.



Please give me example of this statement.


Thanks and Regards,
cmbhatt
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
quote: Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public.

Please give me example of this statement...


Writing your own example would be great practice.

Define an interface with a member type (class or interface) that is not explicitly static or public. Then write code that demonstrates this member type is static (does not require an instance of the enclosing type) and public (can be accessed from another package).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by swarna dasa:


Uh... That's rather explicit. I think we're looking for a demonstration that "public" and "static" are implicit here.
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup.. it is explicit, thought he was questioning the answer (a,f) abstract and public. So had an example with the possible modifiers.

Here is the one (hopefully) exhaustive example

com InterfaceTest.java


test ClassTest.java //name it whatever you want it



Output

>java test.ClassTest
1
2

>java test.ClassNestedClassTest
3
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a couple of examples.

Consider this code.



Notice that the main method is in a static context, and, therefore, you cannot access non-static members of the class from it.

Notice that you can create an instance of Test, the member class inherited from Test1, without any problem.

However, the inner class Test3 from the class Test2 cannot be accessed from the main method without an instance of Test2.

This shows that the class Test defined in the interface Test1 is implicitly static.

To show that the members are implictly public, you can look at the message the compiler gives you if you try to use another access keyword on the member.



If you try to compile this, you get the following error message.

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc, swarna and Keith for your prompt reply.

I got my answer!


Thanks and Regards,
cmbhatt
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic