Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you override private functions defined in a base class?

I believe, a derived class can override only those functions which it inherited from the base class. Is my understanding correct.?

That is if base class has a public member function say, func, then the derived class can override the member function func.

But if the base class has a private member function say, foo, then the derived class cannot override the member function foo.

Am i right?

Edit

I have come up with a code sample after studying the answers given by SO members. I am mentioning the points which i studied as comments in the code. Hope i am right. Thanks

/* Points to ponder:
   1. Irrespective of the access specifier, the member functions can be override in base class.
      But we cannot directly access the overriden function. It has to be invoked using a public
      member function of base class.
   2. A base class pointer holding the derived class obj's address can access only those members which
      the derived class inherited from the base class. */

#include <iostream>

using namespace std;


class base
{
   private:
      virtual void do_op()
      {
         cout << "This is do_op() in base which is pvt\n";
      }

   public:
      void op()
      {
         do_op();
      }
};

class derived: public base
{
   public:
      void do_op()
      {
         cout << "This is do_op() in derived class\n";
      }
};

int main()
{
   base *bptr;
   derived d;

   bptr = &d;
   bptr->op(); /* Invoking the overriden do_op() of derived class through the public 
               function op() of base class */ 
   //bptr->do_op(); /* Error. bptr trying to access a member function which derived class 
                    did not inherit from base class */            

   return 0;
}
like image 641
nitin_cherian Avatar asked Nov 12 '11 10:11

nitin_cherian


People also ask

Can you override a private method?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.

Is it possible to override a private method in Java?

2) In Java, methods declared as private can never be overridden, they are in-fact bounded during compile time.

How do you override a base class function?

To access the overridden function of the base class, we use the scope resolution operator :: . We can also access the overridden function by using a pointer of the base class to point to an object of the derived class and then calling the function from that pointer.

Can we override private function in C++?

C++ has access control, but not visibility control. This means that private functions are visible but not accessible. A private virtual function can be overridden by derived classes, but can only be called from within the base class.


3 Answers

You can override functions regardless of access specifiers. That's also the heart of the non-virtual interface idiom. The only requirement is of course that they are virtual.

like image 180
Xeo Avatar answered Nov 07 '22 07:11

Xeo


But if the base class has a private member function say, foo, then the derived class cannot override the member function foo.

In Java, you can't. In C++, you can.

like image 33
fredoverflow Avatar answered Nov 07 '22 07:11

fredoverflow


You are correct in that to override a function in a derived class, it must inherit it from the base class (in addition, the base class function must be virtual). However you are wrong about your assumption that virtual functions are not inherited. For example, the following works well (and is actually a known idiom for precondition/postcondition checking):

class Base
{
public:
  void operate_on(some thing);
private:
  virtual void do_operate_on(some thing) = 0;
};

void Base::operate_on(some thing)
{
  // check preconditions
  do_operate_on(thing);
  // check postconditions
}

class Derived: public Base
{
  // this overrides Base::do_operate_on
  void do_operate_on(some thing);
};

void Derived::do_operate_on(some thing)
{
  // do something
}

int main()
{
  some thing;
  Base* p = new Derived;

  // this calls Base::operate_on, which in turn calls the overridden
  // Derived::do_operate_on, not Base::do_operate_on (which doesn't have an
  // implementation anyway)
  p->operate_on(thing);

  delete p;
}

A way to see that private methods are really inherited is to look at the error messages generated by the following code:

class Base
{
private:
  void private_method_of_B();
};

class Derived:
  public Base
{
};

int main()
{
  Derived d;
  d.private_method_of_B();
  d.method_that_does_not_exist();
}

Trying to compile this with g++ leads tot he following error messages:

privatemethodinheritance.cc: In function 'int main()':
privatemethodinheritance.cc:4: error: 'void Base::private_method_of_B()' is private
privatemethodinheritance.cc:15: error: within this context
privatemethodinheritance.cc:16: error: 'class Derived' has no member named 'method_that_does_not_exist'

If class Derived wouldn't inherit that function, the error message would be the same in both cases.

like image 28
celtschk Avatar answered Nov 07 '22 07:11

celtschk