• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Restricting access to an object in the same package.

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be totally obvioius, so please bear with me.

I've got two objects that are in the same package. I want a method in one of those objects to only be called by the second object, but no other objects in the package.

For example, I have a FairTales package that contains a Dragon class and a KnightInShiningArmor class. I only want the slayDragon() method of the Dragon class to be called by KnightInShiningArmor objects, but no other objects in the FairyTales package.

Is their an access specifier/keyword that will do this, or is there another technique that I might be missing?

Thanks a bunch.

Landon
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may or may not be what you're looking for. But one way to limit the public interface of a class is by referencing it through the interfaces it implements. For example:
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I suggest the use of nested clasess. Make the KnightInShiningArmor class a public static nested class of the Drangon class, and make private the slayDragon method of the Dragon class.

This code snippet shows how to get what you want. KnightInShiningArmor can slay a dragon, but no other class could.



For instance, King class cannot slay a dragon



But, I knight can always kill a dragon



I hope this helps!

[ May 23, 2006: Message edited by: Edwin Dalorzo ]
[ May 23, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, just put it in the same file as the other class definition, but don't make it public.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Landon Blake:
This may be totally obvioius, so please bear with me.

I've got two objects that are in the same package. I want a method in one of those objects to only be called by the second object, but no other objects in the package.

For example, I have a FairTales package that contains a Dragon class and a KnightInShiningArmor class. I only want the slayDragon() method of the Dragon class to be called by KnightInShiningArmor objects, but no other objects in the FairyTales package.

Is their an access specifier/keyword that will do this, or is there another technique that I might be missing?

Thanks a bunch.

Landon




Yes, declare a private nested interface. You're touching on one of the little known realities that private methods (in Java) violate encapsulation. I can give you examples in open source projects if you like.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic