• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A method in an interface

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is:
Which of the following statements are true?
1) All of the variables in an interface are implicitly static
2) All of the variables in an interface are implicitly final
3) All of the methods in an interface are implicitly abstract
4) A method in an interface can access class level variables
Can anybody explain why 4) is not right?
Thanks in advance!
Yi Dong
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method in any interface is always abstract, it has no implementation. Therefore, it can't access anything.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answers are 2 & 3 only. Am I correct?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, the correct answers are 1,2,3.
JLS:


9.3 Field (Constant) Declarations
ConstantDeclaration:
ConstantModifiersopt Type VariableDeclarators
ConstantModifiers:
ConstantModifier
ConstantModifier ConstantModifers
ConstantModifier: one of
public static final
Every field declaration in the body of an interface is implicitly public,
static, and final.
It is permitted to redundantly specify any or all of these modifiers for such fields.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ans: 1,2,3
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I don't understand why is it static 'implicitly'?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arul kumar:
I'm sorry, I don't understand why is it static 'implicitly'?


A field defined within an interface is implicity static because you can not instantiate an interface. In order to have an instance variable, you need to be able to have an instance of that class. That's not the case with an interface. Therefore, any variables defined within the interface are implicitly static, belonging to the interface itself.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Yi Dong !
A method in an interface can't access class level variables because an interface just provides the contract that a class is suppossed to adhere to. No implementation details are provided in an interface.
Accessing some variable in a method is a question of implementation or definition, which is not allowed to be done in an interface.
Cheers
Anupreet
 
Yi Dong
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys:
Thanks you very much for your explanation!
Yi Dong
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic