File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do u access the classes in interfaces ?

 
Vaibhav Shridish
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do i access the myclass in the interface ?
[ Jess added UBB [CODE] tags to preserve whitespace ]
[ June 19, 2002: Message edited by: Jessica Sant ]
 
Christian Garcia
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding a class definition to an interface is not what you want to do. The interface should
only contain the methods that you want your classes to implement. The class you have
defined in your interface should be declared as another interface or if the implementation
of the 'func()' method needs to be used as a "default" implementation, make that class a top-level abstract class. That way you can extend the abstract class to get the functionality of 'func()' as well as have access to the function signatures defined in your interface.
Hope this helps.
CG
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is perfectly legal to place a class within an interface. Just take a look at the JLS, §9.1.3 Interface Body and Member Declarations and §9.5 Member Type Declarations.
However, the name of the nested class is [interface name].[nested class name]. Therefore, in order to create an instance of your class, you'd have to do this:

Of course, you also have the option of importing that class so you could do this:

I hope that helps,
Corey
[ June 19, 2002: Message edited by: Corey McGlone ]
 
Pierre Post
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is perfectly legal to add a class declaration to an interface body according to the JLS (�9.1.3).
Moreover, default modifier is static, so to instantiate the class you type
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But why would you want to do this?
[ June 19, 2002: Message edited by: Rodney Woodruff ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple (albeit contrived) example:

In this case, I made a Person interface and included with it a nested class that could be used to sort objects of that type using some sort of normal ordering. In this case, my ordering went by last name, then first name.
I'm sure there are better examples, but this is one I came up with off the top of my head.
the advantage of using the nested class is that two classes, which are tightly coupled, can be grouped. This helps encapsulate any functionality pertaining to a Person to a single place.
I hope that helps,
Corey
[ June 19, 2002: Message edited by: Corey McGlone ]
 
Vaibhav Shridish
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanz a lot all ....
got it finally ...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic