Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface

 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maciej Gulak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic