1

Why in C# am I required to specify the access modifier of the method I'm overriding if I'm not changing it? Wouldn't it be simpler and more logical to not specify any access modifier at all in this situations?

(Just to clarify: I write this question not because I think that I'm smarter then language designers, but because I'm sure that they had a good reason that I can't understand yet.)

Edit: I'm not asking about why we can't change access modifier, but rather about why we have to redundantly specify it.

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • 1
    possible duplicate of [Why can't we change access specifier while overriding methods in C#?](http://stackoverflow.com/questions/6236909/why-cant-we-change-access-specifier-while-overriding-methods-in-c) – NotMe Mar 27 '13 at 16:48
  • 1
    @ChrisLively: I don't think it's a duplicate. That questions asks why it can't be changed - this question asks why it has to be specified redundantly. – Jon Skeet Mar 27 '13 at 16:50
  • @JonSkeet: I realized that about 30 seconds after I marked it as such. I was a bit too quick on the closing this time. – NotMe Mar 27 '13 at 16:51

3 Answers3

6

While I'm not one of the language designers, I think it's entirely reasonable to force you to specify it:

  • Anyone reading the code immediately knows the access without having to check the original declaration
  • A change to the access of the original declaration is a breaking change this way, making it much more obvious to the overriding code
  • It makes a method which happens to override a higher declaration consistent with non-overriding methods. (It would be odd for a method with no access modifier to be sometimes public and sometimes private, depending on override.)

Basically, redundancy is sometimes a good thing :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

To illustrate Jon's point in code, consider the following:

class LevelOne
{
    public virtual void SayHi()
    {
        Console.WriteLine("Hi!");
    }
}
class LevelTwo : LevelOne
{
    override void SayHi()
    {
        Console.WriteLine("Hi!");
    }
}
class LevelThree : LevelTwo
{
    override void SayHi()
    {
        Console.WriteLine("Hi!");
    }
}

Ad infinitum. Now imagine you're at n-depth of derived classes, you have to follow the inheritance hierarchy all the way back to the original base class to find out the access modifier of the method. That's just plain annoying!

Ben H
  • 494
  • 3
  • 10
  • Compiler gives error `you can't change access rights` and that's exactly what I wanted to show in the answer. Strange it received to many `dv`. Probably I can't express myself clear enough. – Ilya Ivanov Mar 27 '13 at 16:55
  • 2
    @IlyaIvanov my question is about the hypothetical situation when in overriden method lack of access modifier would signify parent's access level instead of internal. – Max Yankov Mar 27 '13 at 16:57
1

In order to facilitate understanding of the code by another developer who don't write the code. when you read override you know that you make polymorphism

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51