1

I have a package ts, containing interfaces for ticket system. I also have hts package, containing concrete implementation of ts. I have TicketSystem.class in ts, and HTicketSystem.class in hts extended from it the first one. HTicketSystem.class is the only public class in hts package. All other classes in hts have default visibility. hts package has lots of files, among them java beans. I'd like to extract these beans to sub-package hts.beans. The obvious problem then is visibility, because according to JLS:

7.1 Package Members

...

The hierarchical naming structure for packages is intended to be convenient for organizing related packages in a conventional manner, but has no significance in itself other than the prohibition against a package having a subpackage with the same simple name as a top level type (§7.6) declared in that package. There is no special access relationship between a package named oliver and another package named oliver.twist, or between packages named evelyn.wood and evelyn.waugh.

1) If I leave beanA.class with default visibility, it will not be visible to hts classes.
2) If I change beanA.class to public visibility, it will be visible from outside.

Any suggestion/best practice is highly appreciated.

nikis
  • 11,166
  • 2
  • 35
  • 45
Andrey
  • 853
  • 9
  • 27
  • 1
    what's the problem of making them public? – nikis Apr 10 '15 at 09:43
  • Extra visibility. Then one may write something like: ((HTicket)ticket).changeState(.... – Andrey Apr 10 '15 at 09:50
  • or override HTicket to provide custom implementation – Andrey Apr 10 '15 at 09:53
  • to protect from overriding you can declare class/method as `final`. Considering `((HTicket)ticket).changeState(....` it looks like 3rd party consumer got `ticket` object somehow? – nikis Apr 10 '15 at 09:56

0 Answers0