Let's say I have 1 complete class with around 20 methods which provide different functionalities.
Now we have multiple clients using this class, but we want them to have restricted access.
For e.g. -
Client 1 - Gets access to method1/m3/m5/m7/m9/m11
Client 2 - Gets access to method2/m4/m6/m8/m10/m12
Is there any way I can restrict this access?
One solution which I thought:
Create 2 new classes extending Parent class and override methods which are not accessible and throw Exception from them. But then if 3rd client with different requirement, we have to create new subclass for them.
Is there any other way to do this?
You can create an Interface1
which defines methods only for Client1
, and an Interface2
which defines methods only for Client2
. Then, your class implements Interface1
and Interface2
.
When you declare Client1
you can do something like: Interface1 client1
.
With this approach, client1
can accesses only methods of this interface.
I hope this will help you.