Click here to Skip to main content
Licence 
First Posted 24 Jun 2005
Views 20,805
Bookmarked 13 times

Using Interfaces for Advanced Accessibility Control Level

By | 24 Jun 2005 | Article
Using interfaces besides the traditional access modifiers (i.e. public, protected, internal, or private) for advanced accessibility control.

Introduction

It is known that to control the accessibility level of a class member, we have to use one of the access modifiers (i.e. public, protected, internal, or private). However, there are times when it becomes desirable to have different classes with different access rights given to its class members. This article explains a method of doing this by using interfaces and adapter classes. This can be explained by the following scenario:

Let's assume that we have three classes: SharedClass, ClassA, and ClassB and we need an instance of SharedClass to be shared between ClassA and ClassB, but we want ClassA to have access to certain parts of SharedClass instance and ClassB to have access to the other part of the class. These requirements cannot be met by using the traditional access modifiers (i.e. public, protected, internal, or private). So, what is the solution? The solution lies in using interfaces.

Solution

Let's assume that SharedClass has two data members (DataA, DataB) and we want ClassA to control (i.e. to see) only DataAA and ClassB to control DataB. The following code shows the use of interfaces to meet these requirements:

public interface PartA
 {
  void SetDataA(string dataA);
  string GetDataA();
 }
 public interface PartB
 {
  void SetDataB(string dataB);
  string GetDataB();
 }
 public class SharedClass:PartA,PartB
 {
  private string DataA;
  private string DataB;
  public void SetDataA(string dataA)
  {
   DataA=dataA;
  }
  public string GetDataA()
  {
   return DataA; 
  }
  public void SetDataB(string dataB)
  {
   DataB=dataB;
  }
  public string GetDataB()
  {
   return DataB; 
  }
 }
public class ClassA
 {
  public PartA mSharedInstance;
  public ClassA (PartA obj)
  {
   mSharedInstance =obj;
  }
 }
 public class ClassB
 {
  public PartB mSharedInstance;
  public ClassB (PartB obj)
  {
   mSharedInstance =obj;
  }
 }

Now, let's test the code.

As can be seen (Figure1), ClassA can only see certain parts of SharedClass. However, the use Interfaces is not limited to this. See the following code:

public interface PartA
 {
  void SetDataA(string dataA);
  string GetDataA();
  void DoTask();
  void SharedTask();
 }
 public interface PartB
 {
  void SetDataB(string dataB);
  string GetDataB();
  void DoTask();
  void SharedTask();
 }
 public class SharedClass :PartA,PartB
 {
  private string DataA;
  private string DataB;
  public void SetDataA(string dataA)
  {
   DataA=dataA;
  }
  public string GetDataA()
  {
   return DataA; 
  }
  public void SetDataB(string dataB)
  {
   DataB=dataB;
  }
  public string GetDataB()
  {
   return DataB; 
  }
  void PartA.DoTask()
  {
   //some code; 
  }
  void PartB.DoTask()
  {
   //some code;
  }
  public void SharedTask()
  {
   //some code;
  }
 }

As can be seen (Figure2), SharedClass has two methods that have the same name but they define different accessibilities. Moreover, SharedClass has got a shared method that can be accessed either by ClassA or ClassB.

Conclusion

Interfaces are used in C# not only for multiple inheritance but also to have well-defined and advanced accessibility levels to class members.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

evxif



United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNot ingenious PinmemberGates VP18:49 30 Jul '07  
QuestionHow about a demo (semi-real) project and a more intuitive name other than "code"? Pinmemberfwsouthern9:30 24 Jun '05  
AnswerRe: How about a demo (semi-real) project and a more intuitive name other than "code"? PinmemberI Fahdah10:17 24 Jun '05  
GeneralRe: How about a demo (semi-real) project and a more intuitive name other than "code"? Pinmemberfwsouthern12:40 24 Jun '05  
GeneralRe: How about a demo (semi-real) project and a more intuitive name other than "code"? PinmemberI Fahdah22:43 24 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 24 Jun 2005
Article Copyright 2005 by evxif
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid