aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Interface" Watch "Question on Interface" New topic
Author

Question on Interface

Anand Sidharth
Ranch Hand

Joined: Dec 17, 2003
Posts: 44
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
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
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.
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
/**
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.
*/


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Interface