• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do u access the classes in interfaces ?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah thanz a lot all ....
got it finally ...
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic