Search code examples
javac++privatedynamic-binding

Dynamic binding of private methods: Java vs. C++


This is not allowed in Java:

class A { 
    public void method() {}
}

class B extends A { 
    private void method() {}
}

It generates a compile error:

error: method() in B cannot override method() in A 
attempting to assign weaker access privileges; was public

However, this IS allowed in C++:

class A { 
    public:
        virtual void method() {}
};

class B : public A { 
    private:
        void method() {}
};

int main(void) {
    A* obj = new B();
    obj->method(); // B::method is invoked, despite it being private
}

What's the logic behind this behavior in C++?


Solution

  • Remember that the visibility of method is resolved purely at compile-time, C++ has no concept of a runtime verifier. What the compiler sees is a virtual A::method, which is not private. The concrete implementation is declared private, but that is only relevant when this implementation is directly invoked in a manner visible to the compiler, i.e. if you try to access it directly by calling it through B.

    The logic of it is illustrated by the following case: Imagine if B didn't inherit from A publically, but privately — this is allowed in C++, and used when inheritance itself is an implementation detail, such as for a stack class inheriting from vector, but not wanting to expose the vector interface. In that case it would be a feature for B::method to be inaccessible, but A::method working just fine, even if the object is a B instance.

    As Kerrek SB said, here Java is protecting you from one class of mistakes, at the cost of removing legitimate options.