• 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

Question on Interface

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one tell the correct answer for the following question and explian why?
Q: Which of the following are true about the class defined inside an interface
ANS:
1).it is not possible in the java Language.
2).The class is always public.
3).The class is always static.
4).the class methods cannot call the methods declared in the interface.
5).the class methods can call only the static methods declared in the interface.
The answer given was 2, 3 and 4
Can anybody substantiate this answers with explanation???
Thanks, Anand
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anand,
according to JLS :


9.5 Member Type Declarations
Interfaces may contain member type declarations (�8.5). A member type declaration in an interface is implicitly static and public.


Member type here means class or interface. So, referring to the question you were asking about, a class defined inside an interface is implicitly static and public. Now based on this, analyzing the optional answers one by one :


1).it is not possible in the java Language.


is false, because it is possible.


2).The class is always public.
3).The class is always static.


are true, it is implicitly public and static.


4).the class methods cannot call the methods declared in the interface.


is true. Methods declared in an interface is always an instance method. And since the class is static, it will not be associated with any particular instance of the interface, hence it could not access the instance method declared in the interface.


5).the class methods can call only the static methods declared in the interface.


is false. Because interface can not have a static method.
Hope this helps.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/**
Q: Which of the following are true about the class defined inside an interface
ANS:
1).it is not possible in the java Language.
2).The class is always public.
3).The class is always static.
4).the class methods cannot call the methods declared in the interface.
5).the class methods can call only the static methods declared in the interface.
The answer given was 2, 3 and 4
*/
interface InterfaceFunda{
void hello();
abstract class Inner{
abstract void hello();//error
}
}
/**
Hi Anand,
David has already explained urs question very nicely but I am posting this to make further clarification(this is my view :-))Problem with me is that I copy question from the ranch and then go home to read them if I find anything interesting(I don't have net at home).So I am often late in replying.
2 & 3)
Variable inside the methods are always implicitly public and static


4)
JVM always prevent the call to the method whose declaration has not been given yet(this is a reason why JVM doesn't allow a static method to be declared abstract).In the interface only declaration are allowed not the implementation.If u try to call the hello() method of the Interface then compiler will give the error with the assumption that u want to give some method declaration inside the inner but u haven't described that properly(do this and it will ask for the return type).Till u don't put the abstract on the method and the Inner class.
*/
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic