I am trying to implement multiple inheritance. How can i do this

I created a class A that has some functions defined as protected.

Now class B inherits A and class C inherits B. Class A has its own default constructor and a protected parameterized constructor.

I want class B to be able to access all protected functions defined in class A, but class C may have access to some functions, but not all functions and class C inherits class B.

How can I restrict access to some functions of class A from class C?

EDIT:

namespace Db { public class A { private A(){} protected A(string con){assign this value} protected DataTable getTable(){return Table;} protected Sqlparameters setParameters(){return parameter;} } } namespace Data { public class B:A { protected B():base("constring"){} protected DataTable output(){return getTable();} protected sqlparameter values(param IDataParameter[] parameter){} } } namespace Bsns { public class C:B { protected C():base(){} protected DataTable show() {return values(setparameter());} } } 

EDIT

I think I'm trying to do this, this is multiple inheritance.

Please check.

 class A { //suppose 10 functions are declared } class B:A { //5 functions declared which are using A function in internal body } class C:B { //using all functions of B but require only 4 functions of A to be accessible by C. } 
+1
c # oop class class-design
source share
4 answers

I would suggest you reconsider your design. Maybe there is an easier way. What if C uses an instance of B instead of extracting from it (composition)? That way, C can use B public methods, but not access protected ones.

Class A does not have to care about the level / depth of the descendant. If something is marked protected, it must be protected for both B and C (regardless of the depth of the inheritance chain). B may choose to limit its descendants by tightening the restrictions (but this is rare).

If you can tell me more about your context - the problem you are trying to solve. I can give you a more detailed / useful answer.

+1
source share

You need to have classes A and B in one assembly and class C in another assembly. You can mark the member you want to restrict access to derived classes as protected internal . This makes him a member, well, protected and internal. As for restricting class C access to a member, suffice it to note its internal . Since this will make it public in the first build, you can add protected to force encapsulation.

Turns off the labeling of a member protected internal does not make it closed to classes outside the assembly. For all purposes and tasks, it seems that protected internal is the same as protected. Unfortunately, the only way I can achieve this is to note its internal and come to terms with the fact that the member is publicly available for the defining assembly.

Even the C # Programming Guide on MSDN is wrong:

By combining protected and internal keywords, a class member can be marked as protected internal - only derived types or types within the same assembly can access this element.

Phil Haack explains :

protected internal means protected OR internal

This is very clear when you think of keywords as a combination of accessibility rather than intersection. thus, protected interna means that the method is accessible to everyone that the UNION method can access, using protected everything that can access the internal Method.

Here is the updated code:

  class A { protected void Test3(){} //available to subclasses of A in any assembly protected internal void Test() { } //Same as protected :( public void Test2(){}//available to everyone internal void Test4(){} //available to any class in A assembly } class B : A { void TestA() { Test(); //OK } } //Different assembly class C : B { void TestA() { Test4(); //error CS0103: The name 'Test4' does not exist in the current context } } 
+4
source share

It looks like you should probably use a composition that is not inherited.

Class A implements calc () and allow ().

Class B has private A , but not derived from A

Class C comes from B and does not have access to private object A in class B.

+2
source share

As others have said, you probably want to use composition instead of inheritance.

 class A { protected void Foo() { … } protected int Bar() { … } } class B { private A a; public B() { this.a = new A(); } protected int Bar() { return a.Bar(); } } class C : B { … } 

However, looking at your example, I would question if C should inherit from B or should it just simply refer to an object of type B.

Personally, I would not place classes in different assemblies just for the purpose of restricting access, unless the class otherwise logically belongs to another assembly. There are other ways to handle this.

0
source share

All Articles