aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface" Watch "interface" New topic
Author

interface

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Does this statement is true

A class inside an interface is always public and static and cannot call the methods declared in the interface.

if the above is true,then why the below program

compiles without an error,even though the inner class A is abstract.

Can anybody provide me with an example of class inside an interface.
[ March 03, 2005: Message edited by: ramya jp ]
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Ramya,
All interface methods are public and abstract implicitly and the variables are public , static , final. I doubt if there are any rules for inner class within an interface.
[ March 03, 2005: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Maciej Gulak
Greenhorn

Joined: Mar 07, 2005
Posts: 2
Hi ramya,
The statement

A class inside an interface is always public and static and cannot call the methods declared in the interface.

is not true.
For example, this code works:


and in the main(String[]):

The output is: Implemented method() called

So, you can call a method declared in the interface.
Note, that the method() in NestedClass must be marked public,
since it is implicitly declared public in EnclosingInterface
(cannot have weaker access privileges).

Cheers,
Maciej

[ March 07, 2005: Message edited by: Maciej Gulak ]
[ March 07, 2005: Message edited by: Maciej Gulak ]
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by ramya jp:
[QB]A class inside an interface is always public and static and cannot call the methods declared in the interface...compiles without an error,even though the inner class A is abstract.QB]


True. The class inside an interface is always public and static. To prove that it's public, try compiling it with a "private" qualifier. The compiler will tell you that it has to be "public".

To prove it's static (the dreaded static nested class), compile it with the static keyword, and run javap A on it. Look at the results. Then compile it without the static keyword. run javap A on it. Compare the results. It's the same bytecode!!!

To answer your question why your example compiles correctly, you've shown an example of a nested class in an interface. The compiler compiles it as a static and public class. Your class just happened to be an abstract class. It's still public and static. It just happens to be abstract.

If you want to write some code that proves the third point, write (something) like this [I don't have a compiler handy]

In this example I'm trying to call a method in the interface from a static nested class. Compiler won't allow it.

Any class you create in an interface (whether it's an abstract class or a fully implemented class) is a static nested class. Static nested classes can't access any instance methods nor variables from their "enclosing" classes.

I hope that clears that up. Furthermore K+B don't cover classes inside interfaces in their book, that should give you some idea of how relevant it is for the exam (hint: it's not, imho!).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface