Why "private" methods in the object oriented?

OopEncapsulation

Oop Problem Overview


I understand it is a very basic concept in the oops. But still I cannot get my head around. I understood why member variables are private, so class user cannot abuse it by setting up invalid values.

But how can this apply to the methods ?

Oop Solutions


Solution 1 - Oop

Lot of good answers, but maybe one more from a self-taught Java programmer as I went through all that by myself with a lot of pain ;)

Think about a Class as something seen from the outside, not as something you see internally. If you look at a Class from the outside, what you see?

Taking the clock as an example again, a clock can give you info about the current time and it can be set up to show the right time.

So looking at things from the outside, a clock is a machine that can do those two things; public methods we call them.

But we as constructors of this clock we know that before any time operation we have to switch from 23 to 11 on our display (it's that kind of clock), so we have to rearrange things internally a bit to do so. Changing from 23 to 11 works just fine for us in both cases - setting the clock and showing the current time - but we do it "on the side" as the user doesn't have to know about all that complicated math. These are private methods!

So our Clock Class could have two public methods (showTime and setTime) which are all that the user wants to see, and a private method (recountTime) that provides functionality for these public methods and are something that the user doesn't want to see.

So on the one hand, you should keep in mind that private is what won't be reimplemented and accessed by future programmers using your code (as was pointed at in the answers above). But private also means things done on the side, so the user don't see it. That's why we call the public methods a public interface - it's all the user will see from the outside.

For me it is helpful (I'm self-taught, so maybe it's not a very popular methodology...) to write down everything the users (real users and other Classes) will do with my Class (public interface with just public methods' signatures), then to write the signatures of private methods that I-the-implementer will use to accomplish the public goals that promised to provide to my users and then just fulfill it with code.

It can be helpful to keep in mind that the old C rule is still valid (as was expressed in 97 Things Every Programmer Should Know): a function/method should be just a few lines long, really!!

Solution 2 - Oop

Private methods are useful for breaking tasks up into smaller parts, or for preventing duplication of code which is needed often by other methods in a class, but should not be called outside of that class.

Solution 3 - Oop

Methods are (also) used to structure code, and I don't want the internal structure of my implementation to leak out through the interface. Often I have a method which to the outside seems to do a single task, but actually has to perform a couple of smaller tasks. In such cases I make one small private method for each of the subtasks and call them from the publicly visible method.

Solution 4 - Oop

For exactly the same reason - some methods are only intended for use inside the class, and use by non-class objects would be abuse. Think of methods that increment and decrement an object count for the class - these should only be called from a class's constructors or destructor, and so should be private.

Solution 5 - Oop

Well in some cases you want only that specific class to use a method, and protect it from being used by any other class.

Just an example to show how it can be used:

You have a class Clock, it runs and runs keeping track of the time and date. You can get the time or date from it trough public methods. But the clock has to be right. so you can't adjust the time or date from outside of the class. But the clock itself needs to be able to adjust the time (daylights saving time for example)

In this case the Clock will have a private method to adjust the time.

Then you also have an additional pro, which is structuring the code. You can split up your code in smaller private methods which structure the code but prevent them from being used outside your class.

Solution 6 - Oop

Private = Free to modify

The whole issue is other people can't behave. Let me explain.

In a perfect world you would not gain anything by "hiding" stuff from others, you could make all your methods public, tell them which ones to use, and go have fun. But sooner or later someone will rely on a method you wrote, let's say a method that's meant to be internal, just for you, some low level stuff that you expected to be able to change anytime - alas, now you can't, because someone else is relying on it. You change it, and something else breaks. For no visible reason. Because someone didn't use your class as you advised, someone called your inner methods and you improved something in your code.

So the shortest takeaway:

Hiding methods is reserving the right to change them.

Public interfaces should not change, unless it's a major step, like a new version. But the inside world is your playground. No matter how you get things done, as long as you deliver the expected output, it's no one else's business.

So the biggest reason to make methods private is your own freedom. Some will say it's for "security" reasons, but nowadays your code is often visible/editable so obviously we're not talking about access levels here. It's there, but it's not recommended to use. That's what private (and protected) means.

Solution 7 - Oop

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.

Solution 8 - Oop

Having all other answers in mind. It is worth to remember that private method are only a tip for a programmer in many languages. In most cases it is still possible to use private method. For example by creating an object that inherits from object with private method and overrides its method with new public method.

In some modern highly object oriented languages private methods exist only by convention. Method with '_' on beginning is considere to be private.

Solution 9 - Oop

Private methods are those methods which can’t be accessed in other class except the class in which they are declared. We can perform the functionality only within the class in which they are declared. But in C++ they can also access by Friend class.

Public methods are those methods which can be accessed in any class.

Protected methods are those which can be accessed in derived class too.

Thanks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpointlesspoliticsView Question on Stackoverflow
Solution 1 - OoptrzewiczekView Answer on Stackoverflow
Solution 2 - OopuckelmanView Answer on Stackoverflow
Solution 3 - OopThomas LötzerView Answer on Stackoverflow
Solution 4 - OopanonView Answer on Stackoverflow
Solution 5 - OopS.HoekstraView Answer on Stackoverflow
Solution 6 - OopdkellnerView Answer on Stackoverflow
Solution 7 - OopIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 8 - OopsmentekView Answer on Stackoverflow
Solution 9 - OopAshmita ChaudharyView Answer on Stackoverflow