Base class access specifier

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I've several C++ classes that must be ported to C#. Is there any
construction in C# similar to C++:

class A : protected B
{
...
}
 
Stanislav,

Unfortunately, there is not. Anything that is public on the base class
is exposed in the derived class, and only the protected, protected internal
and public (and possibly internal members) are accessibile to the derived
class.

Hope this helps.
 
Unfortunately, there is not.

That's bad news (and perhaps C# language flaw). Thank you anyway.
 
You wouldn't want to hide the public methods of the Object class would
you? It is comforting knowing every object has a ToString() method.
 
Personally I think its a good thing. Inheritance is best used for expressing an is-a relationship not code re-use. Code re-use is all non public inheritance is for and I think encapsulation / delegation is a much more preferable way of handling that. One of the deisgn goals of C# was to remove some of the unnecessary complexity in C++ - I think non-public inheritance was a valid case for removal.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
Unfortunately, there is not.

That's bad news (and perhaps C# language flaw). Thank you anyway.

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.775 / Virus Database: 522 - Release Date: 08/10/2004



[microsoft.public.dotnet.languages.csharp]
 
You wouldn't want to hide the public methods of the Object class would you?

No, in general I would not. But sometimes, I need to hide all methods of the
base class, and this is not possible in C#. I am using C++ for too long time,
perhaps. Consider the following scenario:

I have a class A:

class A
{
int Get(...);
int Set(...);
};

which does whatever it does and servers as a base class for both class B,
which is used internally and class C, which is exposed to be used by 3rd
party. Let's assume that A.Get and A.Set methods are not "safe", and can be
use only by us (in derived class B), but we can provide "safe" limited
functionality to 3rd party (class C). This can be easily done in C++ as:

class C : protected A
{
int GetOnlySomething() { return Get("Something"); }
int SetOnlySomething() { return Set("Something"); }
};

while having

class B : public A
{
....
};

A.Get and A.Set are not available for public use. (A methods could be
protected, but let's say we need them public due to way they are used by us
internally). The point is, that if anyone adds any method to class A, it is
not automatically available in class C, so C "safety" remains.

In C#, you have to implement a new (or override existing virtual) method
with "protected" access modifier, and it will not be automatically done if a
public method is added to class A, which makes class C not to be "safe"
anymore.

You may find this example complicated, but it represents real situation
(designed before I've joined the company :-). I also believe, that the whole
thing could be solved by changing/improving C++ OO design - but using
"protected" base class access modifier in C# would allow me to port the whole
bunch of C++ classes very easily...

Regards,
 
Stanislav Simicek said:
No, in general I would not. But sometimes, I need to hide all methods of the
base class, and this is not possible in C#.

Thank goodness. That would, of course, break the Liskov
Substitutability Principle.
I am using C++ for too long time, perhaps.

Possibly :)
Consider the following scenario:

I have a class A:

class A
{
int Get(...);
int Set(...);
};

which does whatever it does and servers as a base class for both class B,
which is used internally and class C, which is exposed to be used by 3rd
party. Let's assume that A.Get and A.Set methods are not "safe", and can be
use only by us (in derived class B), but we can provide "safe" limited
functionality to 3rd party (class C).

<snip>

This sounds like exactly the reason for the protected modifier. If Get
and Set are only meant to be called from derived classes, they should
be protected.
 
Stanislav Simicek said:
A.Get and A.Set are not available for public use. (A methods could be
protected, but let's say we need them public due to way they are used by us
internally). The point is, that if anyone adds any method to class A, it is
not automatically available in class C, so C "safety" remains.

A better way, which C++ does not have, is to define and expose an interface
to the 3rd party, not the class itself.

-- Alan
 
Stanislav Simicek said:
you?

No, in general I would not. But sometimes, I need to hide all methods of the
base class, and this is not possible in C#. I am using C++ for too long time,
perhaps. Consider the following scenario:

I have a class A:

class A
{
int Get(...);
int Set(...);
};

which does whatever it does and servers as a base class for both class B,
which is used internally and class C, which is exposed to be used by 3rd
party. Let's assume that A.Get and A.Set methods are not "safe", and can be
use only by us (in derived class B), but we can provide "safe" limited
functionality to 3rd party (class C). This can be easily done in C++ as:

class C : protected A
{
int GetOnlySomething() { return Get("Something"); }
int SetOnlySomething() { return Set("Something"); }
};

while having

class B : public A
{
...
};

A.Get and A.Set are not available for public use. (A methods could be
protected, but let's say we need them public due to way they are used by us
internally). The point is, that if anyone adds any method to class A, it is
not automatically available in class C, so C "safety" remains.

In C#, you have to implement a new (or override existing virtual) method
with "protected" access modifier, and it will not be automatically done if a
public method is added to class A, which makes class C not to be "safe"
anymore.

You may find this example complicated, but it represents real situation
(designed before I've joined the company :-). I also believe, that the whole
thing could be solved by changing/improving C++ OO design - but using
"protected" base class access modifier in C# would allow me to port the whole
bunch of C++ classes very easily...

Regards,

Hiding the base members wrecks the is-a relationship of inheritance. An
elephant that no longer has a trunk isn't an elephant anymore.

Use interfaces to selectively expose members to consumers. The interface
gives the outside world a specific "view" or subset of the total members of
the class(es).

-- Alan
 
Back
Top