In different design books I have read that

  1. First identify the purpose of class (abstraction).
  2. Class should only do one thing (SRP).
  3. methods are defined as responsibility of the class.

As per my understanding public methods handle the responsibilities of the class which require other class [ie interacting classes]

My question is what responsibilites private members of the class do? and how to decide which method to be made private?

Have a look at my code

class calculator
    long double operand_1;
    long double operand_2;
    long double result;

    int optr;
    int multiplier;

    Button One;
    Button Two;
    Button Three;
    Button Four;

    const long double get_number_operator(const int=0);
    const long double calculate_number(const int [],const int);

    void show_calculator( );
    void calculations( );
    void clear_screen( );
    void show_back(const int);
    void show_about( );
    void show_time( );

The method in public are really public or we can put those in private?


4 Answers 4


Private members come in two flavours:

  • Private data members are used to store the internal state of the object
  • Private methods can be seen as named blocks of code that can be used by other methods of the class to fulfill their responsibility.
    Private methods are typically used when several methods need to do the exact same work as part of their responsibility (like notifying external observers that the object has changed), or when a method is split in smaller steps for readability.

A method should be made private if other classes would have no reason to know that the method exists.

  • 1
    The other use for private methods is to provide an abstraction. isEmpty() rather than size==0 Aug 3, 2017 at 11:25
  • I have updated the question with code, can you update me the methods I keep in public are really public? like show_calculatorobv need GUI class to interact, so we put show_calculator in public? Aug 3, 2017 at 11:31
  • @NarenderParmar, As your class is designed, it is hard to tell if all methods are correctly declared public, because your class seems to have multiple responsibilities (handling the display, performing calculations, showing about screens). Aug 3, 2017 at 11:40
  • @Bart van Ingen Schenau - okay cool!! so I need to split my class responsibility, thanks for updating Aug 3, 2017 at 11:47
  • 1
    A method should be made private if other classes would have no reason to know that the method exists. and no reason to change it. Private methods also protects sensible code. If altering a method, the class miss its reason to change (SRP) , the access to such method should be as constrained as possible.
    – Laiv
    Aug 3, 2017 at 14:21

The public members are the interface through which the outside world interacts with this object.

The private members are implementation details that describe how a class fulfils its purpose. Since they are private they do not affect how other code interacts with our class and are therefore not part of a larger design. The private members are used to implement the public methods. So basically: any member you create should be private if it is not needed for the external interface of that class.

Sometimes private methods implement some functionality that could be modelled as a responsibility in its own right, could be re-used in another context, or is so complex that you want to unit-test it directly. It can then make sense to extract these methods into free functions or into a class of its own. You often end up with a number of utility classes that have nothing to do directly with the core business logic of your software, but are needed by that business logic to get the job done. For example: methods for loading data from files, abstract algorithms, or adapters to other systems. Quite often, you can use existing libraries for these tasks.

  • @amon-i m about to add the unit test part, you explained it a way, can you please elaborate it will be really helpful Aug 3, 2017 at 11:22

If you want either inherit these methods in derived class or reuse by other classes then declare them as Public. And if you protect it from being used by any other class then declare them as Private. Because methods that are private can only be called by methods within the same class or within the same "module". Methods are not commonly made private; usually they're made protected so that children can call them, or public so that other code can call them.


If someone else needs to call a method, it must be public. If nobody else needs to call it, you make it private. And the opposite of course: Only code inside your class can call private methods; anyone can call public methods.

Imagine your boss tells you what your class needs to be able to do, and how and with which interface this functionality is called. That gives you your public methods. Everything else is private.

Not the answer you're looking for? Browse other questions tagged or ask your own question.