Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

interface

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe you can define a class inside an interface.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
ice is for people that are not already cool. Chill with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic