Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface

 
puneet pruthi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe you can define a class inside an interface.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic