aspose file tools*
The moose likes Java in General and the fly likes 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 » Java » Java in General
Bookmark "interface" Watch "interface" New topic
Author

interface

puneet pruthi
Greenhorn

Joined: Feb 10, 2001
Posts: 14
Hi there!!
I read this some where and i want to clear my doubts abt it.Can i define a class inside an interface?If yes where can i use itor where do i use it for?
thank you.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
I don't believe you can define a class inside an interface.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I don't believe either of you have tried. Do so. You can indeed create a class inside an interface. If you consult the JLS, you will find that such a class is implicitly static, and public. So it's a static member class, even if it wasn't declared as such. What's it good for? Good question - I suppose it's tied to "what are atatic member classes good for in general?" Offhand, I suppose I might use it to define exception classes associated with the interface I am designing:
<code><pre>
interface Parser {

parse(File input) throws Parser.Exception;

class Exception extends java.lang.Exception {
// some methods
}
}
</pre></code>
Now I'm not arguing that's particularly useful, but it's legal.

[This message has been edited by Jim Yingst (edited July 10, 2001).]


"I'm not back." - Bill Harding, Twister
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Thanks Jim. I would never have imagined it could be done. I'll try it next time before I open my big mouth.
Christophe Lee
Ranch Hand

Joined: Jul 10, 2001
Posts: 142
Question: if you had a static member class (say, in an interface)and used it to define the interface's exceptions, doesn't that mean the (inner, static, member) exception class can't extend or implement another class? And don't all exception classes have to extend some exception superclass?
Or am I confused beyond recovery?
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Christopher,
Nested classes, including static member classes of interfaces, can extend any class. It is legal to do this:
<pre>
public interface Foo {
public static class Bar extends Exception { ... };
public void baz() throws Bar;
}
</pre>
That said, it's generally not a good idea for interfaces to contain classes.

------------------
Joshua Bloch
Author of Effective Java


Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Oops - my previous example omitted the "extends java.lang.Exception", which was of course the intended purpose (as per Josh's subsequent example). I have fixed the earlier article.
"And don't all exception classes have to extend some exception superclass?"
Pretty much. I mean, it's legal to create a class called "Exception" which does not extend java.lang.Exception, but it's at best a misleading name for me to give it. Such a class would not be an exception as the term is used in Java. Bad idea; not my intent.
[This message has been edited by Jim Yingst (edited July 10, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface