Back to list
Views:   14.2K
Replies:  3

How to control access to Interface implementations

I have a question related to Interfaces.

Suppose I have two interfaces (Interface1 and Interface2) and each has three different methods. I am implementing both interfaces in a class named ClassY.

My question is this: I want to expose only those methods that belong to Interface1 to the outside world, whereas the members of Interface2 are to be used only by the classY itself.

What is the proper technique to achieve such a functionality?

Vishal Srivastava, Jun 29, 2011
Reply 1
Tarriq is right, the whole purpose of having a class implement an interface is to allow that class to be used in all the code that would accept that interface.

In your case it sounds like what C++ calls private subclassing, you can achieve that simply by using aggregation/composition, or mark as private the methods that would otherwise implement Interface2.

Also ask yourself: what's the purpose of Interface2 if the class that implements it will not expose the methods?

I class Y implements interface I1 then the objects of class Y will be sent to methods like:

void DoSomething( Interface1 i1);

where the DoSomething method is unaware of the concrete object Y but knows what Interface1 is.

This is the Liskov substitution principle from S.O.L.I.D:

If another method would normally accept Interface2

void DoSomethingElse( Interface2 i2);

then by making the I2 methods private in class Y the DoSomethingElse method will not be able to accept an object of class Y which means that class Y has no business implementing Interface2.

Maybe what you need is something like this:

class Z : Interface2

class Y : Interface1
     Interface2 _i2;
     public Y( Interface2 i2)
        _i2= i2;

then before you create class Y you do:

Interface2 i2 = new Z();

Interface1 i1 = new Y(i2);

This also obeys the Single Responsibility Principle.

And one day you may implement class Z2 that implements Interface2 but in a different way, you create decoupling (which is good) and give your code openess and easy maintenance which is also good. Your class Y will not have to change if you throw away the class Z and replace it with Z2.

Danut Prisacau, Aug 18, 2011
Reply 2

Ideally when you provide an implementation to an interface's method, there should essentially be a need that the implementation is to be used by the clients to your class Y.

If you require to hide that implementation then there should be some serious issue on the Code design.

Could you please explain why do you want to hide the implementation of Interface2 on Class Y to outside world?

Tarriq Ferrose Khan
Tarriq Ferrose Khan, Aug 15, 2011
Reply 3
Given two interface A1, A2 and an implementing class Y

public class Y : A1, A2 {
  // implementation goes here

Outside the class

A1 instance = new Y();  // will only expose methods declared by A1 interface.  In real scenario this could be dynamically fetched

Is this what you are looking out for?
Rajesh Pillai, Jun 29, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.