[Issue 2524] final override inconsistent when implementing interfaces

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 20 14:40:21 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2524





------- Comment #9 from 2korden at gmail.com  2009-01-20 16:40 -------
(In reply to comment #7)
> In fact, private implies final (non-virtual), and does not put the function in
> the vtable (meaning it cannot override a base function).

Protection should be orthogonal to "virtuality" attributes. Here is an example:

class A
{
    private void foo() {
        writefln("A.foo");
    }
}

class B : A
{
    private override void foo() {
        super.foo();
        writefln("B.foo");
    }
}


> From the spec: "All non-static non-private non-template member functions are virtual."

I believe this is a design flow. Same as "package implies final".

> If someone casts your class to an interface, do you want them to now
> be able to call your private function?
> 

Of course, why would you inherit or override it other than to allow virtual
behavior? Here is an example:

interface INetworkListener {
    void acceptNetworkPacket();
}

class NetworkManager
{
    static void registerNetworkListener(INetworkListener listener) { ... }
}

class SoundManager : private INetworkListener
{
    this() {
        NetworkManager.registerNetworkListener(this);
    }

    private void acceptNetworkPacket() {
        // ...
    }
}

No-one should know that SoundManager implements INetworkListener interface.
Since "acceptNetworkPacket" method is an implementation detail, I don't want it
to be visible from the outside of this class (to prevent accidential
invokation). Thus I mark it private.

Note that private interface inheritance is harmless as it doesn't shadow
Object's methods (opCmp, opEquals, toString etc).


-- 



More information about the Digitalmars-d-bugs mailing list