Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Interfaces?

 
Nick Howell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I need to implement an interface that I made inside a nested class:
public class aClass {
class aNestedClass implements anInterface {
}
}
I can apply an interface to a public class by making the public interface and compiling into a package and then importing the package but it won't allow me to apply it to the innerclass. Is there someway I can implement an interface in an innerclass?
Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have any problem compiling this code. How have you defined anInterface? What error message do you get? Are you using an older JDK?
 
Nick Howell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well what my problem is, is that I can compile the interface as public in it's own .java file and put it in a package. I can import this package into my code and implement the interface into a public class but not a non-public class. The reason is that the class's scope must match that of the interface. So what i need is an interface with a scope that isn't public so i can apply it to an inner class.
Thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick Howell:
I can import this package into my code and implement the interface into a public class but not a non-public class. The reason is that the class's scope must match that of the interface.

Huh? I never heard of this. Can you please post example code that doesn't compile?
 
Nick Howell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aMethod in aPublicClass.aClass.aClass cannot implement aMethod in package.anInterface; attempting to assign weaker access privleges; was public
class aClass implements anInterface {
^
 
Nick Howell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the preceeding post was the error message i received by the way.
Thanks!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, make the new method public. You haven't shown any methods at all in the example so far, so it's hard for us to guess what you're talking about.
 
Nick Howell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that worked! I misinterpreted the requirement, it's not the class that has to be the same scope as the interface but rather the methods in the interface! I changed all the instantiated methods to public. thank you for your patience.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic