abstract method override in Derived class, how to make private
Asked Answered
E

3

8

Hi I have a class "A" with as abstract method

protected abstract List<Contributor> GetContributors(List<SyndicationPerson> contributersList);

I want to override this method in derived class "B" with following conditions

  • It should be private to B class.

compiler does not allow me to declare this Method as private in derived class "B" what is the correct syntax ?

Expatriate answered 29/11, 2009 at 18:54 Comment(1)
Why do you want to this?Puree
M
10

You can't. That would violate the accessibility level declared in class A. Aside from anything else, it would stop it from being callable by class A! What would you expect to happen if code in class A tries to call the abstract method which you'd somehow overridden with a private implementation?

You can make the main implementation private and then create a protected method which just calls the private one, if you really want to.

Why do you want to make the method private in the first place, when it's designed to be callable from A?

EDIT: Okay, now you've explained in your comment what you want to do, you can't do it. The closest you can come is to pass a delegate to A's constructor - that delegate can refer to a private method. Unfortunately, you can't use "this" when you pass arguments in constructor chains, so you're forced to do something horrible such as writing a static method which takes "this" as its first parameter, effectively... except that it will have to cast it to the right type as well, as the parent can't declare which type it should be. The parent would then call the delegate instead of the protected method.

Note that this would also prevent further derived classes from "overriding" further, which may or may not be desirable.

It's incredibly tortuous, and I'd try to avoid it wherever possible. If you're really worried about what derived classes might do, I'd try to seal the class instead and force people to use composition instead of inheritance. The language doesn't really help you do what you want to here.

Metcalf answered 29/11, 2009 at 19:0 Comment(2)
Thanks Jon, got the point, I think I haven't made my self clear. I want the overridden method "GetContributors" in class "B" callable inside class "B", callable from Base class "A", but not to be inheritable further, not callable in inherited class, say "C", not callable by using an object of another class like other public methods Can you tell what are the implications using for using this syntax. It compiles though and seems to fulfil some of the requirement as described earlier. ----- protected sealed override List<Contributor> GetContributors(List<SyndicationPerson> contributersList----Expatriate
Do appreciate jon, saved my day.Expatriate
C
2

As a general OOPS rule, one cannot reduce the visibility of a member when overriding. So going from protected to private is not allowed.

Cinda answered 29/11, 2009 at 19:0 Comment(2)
Note that this is overriding, not overloading. (You actually can reduce visibility when overloading, of course :)Metcalf
Thanks for the correction, I somehow always end up making that mistake. :)Cinda
C
0

You cannot change access level of virtual methods in C#. You can redeclare it by using the new keyword, but this is a redeclaration that hides the previous method, so it will not work as you expect if you're dealing with objects that differ in declared and actual type, or if you routinely cast objects to a base class type.

Culpable answered 29/11, 2009 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.