Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow widening of accessors for overridden methods #4672

Closed
drake7707 opened this issue Sep 6, 2015 · 2 comments
Closed

Disallow widening of accessors for overridden methods #4672

drake7707 opened this issue Sep 6, 2015 · 2 comments

Comments

@drake7707
Copy link

class Test {
    protected doSomething() {
    }
}
class SubTest extends Test {
    doSomething() {
        super.doSomething();
        alert("Something");
    }
}

This will give an error:

let test:Test = new SubTest();
test.doSomething();

This will not:

let test:SubTest = new SubTest();
test.doSomething();
@benliddicott
Copy link

Is your suggestion that it should not be possible to override a protected method with a public method?

If so, I think that will cause problem - in fact we have people like me asking for the opposite:

#471 Permit in implementing class to ignore private methods of implemented interface
#3854 Allow interface implementors to implement protected methods
#3598

@drake7707
Copy link
Author

Ah you bring up good points. I'm so used to writing C# where subclasses can't change the accessors of methods it's overriding that I found myself thinking that this might not be intended behaviour.

I thought about it some more and looked things up (it seems Java does allow this behaviour), e.g http://stackoverflow.com/questions/23081671/why-can-i-override-a-protected-method-with-public-method , http://stackoverflow.com/questions/10458142/why-a-function-with-protected-modifier-can-be-overridden-and-accessible-every-wh , http://stackoverflow.com/questions/12780779/why-java-allows-increasing-the-visibility-of-protected-methods-in-child-class

So in hindsight this might not be the best idea, I do wonder why C# does not allow this hmmm, something to read up on.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants