Private visible?
Bruno Medeiros
brunodomedeirosATgmail at SPAM.com
Fri Jul 14 07:01:00 PDT 2006
Jari-Matti Mäkelä wrote:
> xs0 wrote:
>> class Number {
>> public void setVal(int val) { ... }
>> public void setVal(long val) { ... }
>> }
>>
>> class IntNumber : Number {
>> public void setVal(int val) { ... }
>> private void setVal(long val) { assert(0); }
>> }
>>
>> Number a = new IntNumber();
>> a.setVal(10L);
>>
>> Now, not only does the last line compile, it also calls the wrong
>> function and fails to fail.
>
> And casting back to IntNumber makes it again private. No OOP language
> should do that. It's against the rules of polymorphism.
>
Agreed, it does seem that contravariant (method) protection levels
should not be allowed.
>
>> As for overloading issues, as far as I am concerned, feel free to
>> require all methods with the same name to have the same protection;
>> anything else is poor taste anyway.
>
> Really? Then how do you hide the previous method implementation?
>
> class A {
> protected pm() { ... }
> }
> class B : A {
> // ugly, isn't it :(
> protected pm() { assert(0); /* subclasses: do not use this */ }
> public pm2() { super.pm(); ... }
> }
>
> vs.
>
> class A {
> protected pm() { ... }
> }
> class B : A {
> public pm() { super.pm(); ... }
> }
>
He said overload, not override.
--
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list