Force an interface instead of a specific implementation in a declaration (.NET)

In C ++ you can do the following:

class base_class { public: virtual void do_something() = 0; }; class derived_class : public base_class { private: virtual void do_something() { std::cout << "do_something() called"; } }; 

derived_class overrides the do_something() method and makes it private . The effect is that the only way to call this method is:

 base_class *object = new derived_class(); object->do_something(); 

If you declare an object of type derived_class , you cannot call the method because it is private:

 derived_class *object = new derived_class(); object->do_something(); // --> error C2248: '::derived_class::do_something' : cannot access private member declared in class '::derived_class' 

I think this is pretty good, because if you create an abstract class that is used as an interface, you can make sure that no one accidentally declares a field as a specific type, but always uses an interface class.

Since in C # /. NET in general, you are not allowed to restrict access from public to private when overriding a method, is there a way to achieve a similar effect here?

+6
c ++ inheritance access-modifiers c #
source share
4 answers

If you explicitly implement the interface, this will at least encourage people to use the type of interface in the declaration.

 interface IMyInterface { void MyMethod(); } class MyImplementation : IMyInterface { void IMyInterface.MyMethod() { } } 

One of them will only see MyMethod after the instance instance is IMyInterface . If the declaration uses an interface type, casting is not required for subsequent purposes.

MSDN page on explicit interface implementation (thanks to Luke, I save a few seconds ^^)

 IMyInterface instance = new MyImplementation(); instance.MyMethod(); MyImplementation instance2 = new MyImplementation(); instance2.MyMethod(); // Won't compile with an explicit implementation ((IMyInterface)instance2).MyMethod(); 
+14
source share

You can also do this in the .Net world using an explicit interface implementation

As an example, a BindingList<T> implements an IBindingList , but you must send it to an IBindingList to see the method.

+4
source share

You can reduce the availability of a method by marking it as new .

An example from MSDN CA2222: do not reduce the visibility of an inherited element :

 using System; namespace UsageLibrary { public class ABaseType { public void BasePublicMethod(int argument1) {} } public class ADerivedType:ABaseType { // Violates rule: DoNotDecreaseInheritedMemberVisibility. // The compiler returns an error if this is overridden instead of new. private new void BasePublicMethod(int argument1){} } } 

This is really more interesting as an academic exercise; if your code really depends on the inability to call BasePublicMethod on ADerivedType , this is a warning sign of dubious design.

+2
source share

The problem with this strategy, if implemented, is that the method is not truly private. If you want to improve the quality of the base_class , then the method will become publicly available. Since this is a virtual method, user code will execute derived_class::do_something() eventhough, but it will be marked as private.

0
source share

All Articles