P
Peter Oliphant
I use to make the statement that you could always replace any 'private' or
'public' access with 'public' and the code would still compile. This is no
longer the case.
It turns out that in /clr it is not allowed (as far as I can tell) to reduce
the access of a virtual method defined in a base class. Thus, if one
declares a virtual method in a base class as 'protected' and then proceeded
to create derived classes with the virtual method polymorphed as 'protected'
methods, then changing the base class version to 'public' will now cause all
the derived classes to generate compiler errors! This is true of virtual
abstract methods as well...
Why is limiting access of a polymorphed virtual methods no longer allowed? I
speak here of ref classes compiled using /clr. It seems it would be a very
good practice to create a base class with methods a child could use but
might not want this method changeable in any derived grand-children. This is
especially true with abstract methods, as it is very reasonable to create a
'line' of derived classes with a particular abstract method fixed. This
appears to no longer be the case, since if you make the abstract method
accessible to its immediate child it must now also be polymorphable by all
the grand-children, and all the grand-children and children of any other
'lines' derived from the base class...
I thought that was the reason for not making everythin 'public' in the first
place - the abilty to limit access. And it's always been a good rule to
apply the 'principle of least accessibility'. Now this can be a real chore
to maintain, as its possible (like in the project I'm doing now) to have
hundreds of 'concrete' classes derived from a given base class, and then
watch out if you need to change the accessibility of a method or member in
the base class...!
I'm guess there are new ways to do this, but I haven't seen any mention of
this...
[==P==]
'public' access with 'public' and the code would still compile. This is no
longer the case.
It turns out that in /clr it is not allowed (as far as I can tell) to reduce
the access of a virtual method defined in a base class. Thus, if one
declares a virtual method in a base class as 'protected' and then proceeded
to create derived classes with the virtual method polymorphed as 'protected'
methods, then changing the base class version to 'public' will now cause all
the derived classes to generate compiler errors! This is true of virtual
abstract methods as well...
Why is limiting access of a polymorphed virtual methods no longer allowed? I
speak here of ref classes compiled using /clr. It seems it would be a very
good practice to create a base class with methods a child could use but
might not want this method changeable in any derived grand-children. This is
especially true with abstract methods, as it is very reasonable to create a
'line' of derived classes with a particular abstract method fixed. This
appears to no longer be the case, since if you make the abstract method
accessible to its immediate child it must now also be polymorphable by all
the grand-children, and all the grand-children and children of any other
'lines' derived from the base class...
I thought that was the reason for not making everythin 'public' in the first
place - the abilty to limit access. And it's always been a good rule to
apply the 'principle of least accessibility'. Now this can be a real chore
to maintain, as its possible (like in the project I'm doing now) to have
hundreds of 'concrete' classes derived from a given base class, and then
watch out if you need to change the accessibility of a method or member in
the base class...!
I'm guess there are new ways to do this, but I haven't seen any mention of
this...
[==P==]