What should be the access modifier of ServiceImpl Class?

62 Views Asked by At

A class CorporateServiceImpl is implementing an interface called CorporateService. Here I know that the CorporateServiceImpl class never be used outside of the package. I will use the implemented method of the CorporateServiceImpl through the interface. In that case, what should be the access modifier of the CorporateServiceImpl class? Currently, I am preferring protected so that other classes of the package could use this. Is my implementation correct? Or it should be public?

2

There are 2 best solutions below

4
Azadi Yazdani On

You always have to use at least one Public Class in your java file, otherwise it won't be compiled to .class file in JVM. Only methods and fields are behaved as private or protected. If you want to have a private or protected class for example, you need to declare them in another Public class.

0
Ahmed Nabil On

totally up to you, if you will not use it outside the package, use default modifier.