File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How do u access the classes in interfaces ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How do u access the classes in interfaces ? " Watch "How do u access the classes in interfaces ? " New topic
Author

How do u access the classes in interfaces ?

Vaibhav Shridish
Greenhorn

Joined: Jun 06, 2002
Posts: 28

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 ]

Vaibhav Shridish
Christian Garcia
Ranch Hand

Joined: Jan 29, 2002
Posts: 77
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


Christian
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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 ]

SCJP Tipline, etc.
Pierre Post
Greenhorn

Joined: Jun 18, 2002
Posts: 12
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


SCJP2
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80

But why would you want to do this?
[ June 19, 2002: Message edited by: Rodney Woodruff ]

Hope This Helps
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Jun 06, 2002
Posts: 28
Yeah thanz a lot all ....
got it finally ...
 
Consider Paul's rocket mass heater.
 
subject: How do u access the classes in interfaces ?
 
Similar Threads
Overriding rules... Please confirm!
anonymous class?
How to call a generic method with two bounds (for a single argument) if one bound needs a cast
1 error when compiling PlayCode.java
Anonymous class behavior confusion !