It is impossible, why?
In C #, it is imposed on you that if you inherit publicly available methods, you must make them publicly available. Otherwise, they expect that you will not extract from the class in the first place.
Instead of using is-a relationships, you will need to use has-a relationships.
Language developers do not allow this specifically so that you use inheritance more correctly.
For example, you might accidentally confuse the Car class to get from the Engine class to get its functionality. But Engine is the functionality that the car uses. So you would like to use a has-a relationship. The Car user does not want access to the Engine interface. And the car itself should not confuse the methods of the Engine with its own. Nor Car of future derivations.
Therefore, they do not protect you from incorrect inheritance hierarchies.
What should you do instead?
Instead, you should implement interfaces. This gives you the opportunity to have functionality using has-a relationships.
Other languages:
In C ++, you simply specify the modifier in front of the base class private, public or protected. This makes all members of the database available for this access level. It seems silly to me that you cannot do the same in C #.
Modified Code:
interface I { void C(); } class BaseClass { public void A() { MessageBox.Show("A"); } public void B() { MessageBox.Show("B"); } } class Derived : I { public void C() { bA(); bB(); } private BaseClass b; }
I understand that the names of the above classes are a bit controversial :)
Other offers:
Others suggested making A () and B () public and throwing exceptions. But this does not make a friendly class for people, and it does not make sense.
Brian R. Bondy Sep 19 '08 at 23:56 2008-09-19 23:56
source share