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?
gammelgul
source share