views:

261

answers:

7

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 ?

+3  A: 

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.

anon
+18  A: 

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.

uckelman
I'm not quite sure how private methods have any direct correlation to breaking tasks up or preventing duplication of code. What we are talking about here is method scope.
Jeremiah
Suppose that you have a group of preconditions which you need to validate before calling various methods in your class, or a lock which needs to be acquired, or really any block of code which would be repeated in multiple methods, but has no sense outside of the class. This code is a perfect candidate for being put into a private method, exactly because it's repeated and has no sense outside of the class. This latter reason _is_ about method scope.
uckelman
+1  A: 

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.

Ignacio Vazquez-Abrams
+8  A: 

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.

Thomas Lötzer
I agree, and I would say that this is also the main reason for having private member variables too. The fact that you then can validate any changes to them to avoid invalid values I see as a byproduct of this.
ho1
+1  A: 

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.

S.Hoekstra
+5  A: 

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 class as something seen from outside, not as something you see internally. If you look at a class from outside, what you see? Taking the clock as an example again, clock can give you info about the current time and it can be set up to show a right time. So looking at things from outside, clock is a machine that can do 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 such a kind of the 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) - all the user WANTS to see - and a private method (recountTime) - all we need to provide good functionality of these public methods and all the user really DOESN'T WANT to see.
So on the one hand you should keep in mind that private is all that won't be reimplemented and accessed by future programmers using your code (as was pointed in answers above). But private means as well "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 outside. For me it was very helpful (i'm a self-taught so maybe it's not 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-implementor will use to acomplish the public goals that promised to provide to my users and then just fullfill 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 Shoud Know): a function/method should be just a few lines long, really!!

A: 

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.

smentek