0

Suppose I have a super class that offers a public template method. Subclasses will have to implement some sub-operations. How do I declare this sub-ops to make sure they can only be called from SuperClass? There's protected, but that works the other way round as far as I know: Subclasses can access protected superclass members. I want to allow a superclasses (and only superclasses!) to call subclass members.

class SuperClass{
    public:
      void templateMethod(){
         this->op1();
         this->op2();
      } 

      // how to declare these? public? protected?
      virtual void op1() = 0;
      virtual void op2() = 0;
}

class SubClass : public SuperClass{
      // how to declare these? public? protected?
      virtual void op1() { ... };
      virtual void op2() { ... };
} 

I'm currently working in C++ and Matlab, but I'd also be very interested in some general remarks considering other languages.

Michael
  • 7,407
  • 8
  • 41
  • 84
  • 1
    In C++, they can be public, protected or private. It really depends on whether you want to expose them in your base class. I usually go for `private` unless there are good reasons to do otherwise. But you should restrict this to one single programming language unless you know for sure that the behaviour is the same in all. – juanchopanza Mar 07 '16 at 10:51
  • A `private` method can be called from superclasses? Didn't know that. But it makes sense since they are `virtually` declared in that superclass. – Michael Mar 07 '16 at 10:53
  • 1
    Yes, it is a common pattern. The "template method" or something like that. Some people use `protected`, usually for no good reason, but `private` will work fine. – juanchopanza Mar 07 '16 at 10:55
  • 1
    Your question is to broad, no one can explain visibility rules for a unknown set of programming languages. I recommend to update the question and ask only about C++ first. If you later have problems adapting the concept to another language, ask a new question. – Daniel Mar 07 '16 at 11:01

3 Answers3

1

use "private" in both cases

A private method can be called from superclasses because Supercalss don't know about accessible of overrided methods in SubClass

nikniknik2016
  • 348
  • 2
  • 8
1

In C++, you can achieve your aim by making op1 and op2 private in all classes. This technique is idiomatic in C++.

A base class will access the private function in a child class through the v-table that is set up when an object is instantiated.

Of course, there is nothing stopping another function in your child class from calling a function marked private in that child class. If you want wanted to do that, then the child class would not be the correct place for the function.

The same can be said for Java, except, of course that virtual is automatic.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0
class SuperClass{
    public:
      void templateMethod(){
         op1();
         op2();
      } 

    private:
      virtual void op1() = 0;
      virtual void op2() = 0;
}

No need to write private, but it is nice to give some information at the interface.

class SubClass : public SuperClass{
      virtual void op1() { ... };
      virtual void op2() { ... };
} 

is equal to

class SubClass : public SuperClass{
    private:
      virtual void op1() { ... };
      virtual void op2() { ... };
} 
mustafagonul
  • 1,139
  • 1
  • 15
  • 32