0

There is one class named ClassA, it has one public function named FunctionA.

There is another class named ClassB, it needs to use FunctionA and it is not a subclass of ClassA.

The third one named ClassC, FunctionA should not be called by ClassC and it is not a subclass of ClassA.

In addition, the relationship between ClassB and ClassC is not inheritance.

If there are some solutions provided? or if there are suitable design patterns?

Thanks for help.

Ivan
  • 661
  • 1
  • 8
  • 15
  • [Here](http://stackoverflow.com/questions/182278/is-there-a-way-to-simulate-the-c-friend-concept-in-java) is a related question which might help you. – Jorn Vernee Mar 30 '16 at 10:42
  • Hi @JornVernee, you helped me. From your link, I got the most appropriate answer from Salomon BRYS. Thanks. – Ivan Apr 04 '16 at 09:48

2 Answers2

3

You can place ClassA and ClassB in the same package (and ClassC in the other) and use package-private (or default) access modifier for method FunctionA.

This solution is the easiest and uses only JLS specs (works for any language level and on any JVM implementation):

Example 6.6-4. Access to Package-Access Fields, Methods, and Constructors

If none of the access modifiers public, protected, or private are specified, a class member or constructor has package access: it is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package.

Other ways to deal with your problem - reflection, code generation etc - is much more complex, buggy and slow

PS: also it is possible to leave method FunctionA public and decompose your application into two modules. In the first module you should place ClassA and ClassB and in the second ClassC. First module can use the second as a dependency but the second one shouldn't have an access to the first. This way is more suitable for complex applications and I recommended to use build tools such as Maven or Gradle to handle with such oriented dependency graphs (may be very tricky for large scale apps)

Community
  • 1
  • 1
Cootri
  • 3,806
  • 20
  • 30
  • I can not get the point of the last part which tells 'decompose application ...', could you provide some key codes. In addition, what I more want to solve is that Class A and Class B are not in the same package. @Cootri – Ivan Mar 30 '16 at 10:43
  • yeah, just google **"maven multi module project"** for a starting point then. There are plenty of examples in the Web: http://www.codetab.org/apache-maven-tutorial/maven-multi-module-project/ , http://www.mastertheboss.com/jboss-frameworks/maven-tutorials/jboss-maven/maven-multi-module-tutorial and so on – Cootri Mar 30 '16 at 10:46
1

Maybe the Interface segregation principle, one of the SOLID principles, can solve your issue.

Make class A implement an interface AB that is used by class B and an interface AC that is used by class C. In this way class C doesn't see the methods provided for class B.

Glenner003
  • 1,450
  • 10
  • 20