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 ?
Variables and methods defined with the private keyword may be accessed only by other methods within the class and cannot be accessed by derived classes. The private keyword is used in most object-oriented programming (OOP) languages, including C++, C# and Java.
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.
Object users can't use private methods directly. The main reason to do this is to have internal methods that make a job easier.
In Java private methods are the methods having private access modifier and are restricted to be access in the defining class only and are not visible in their child class due to which are not eligible for overridden. However, we can define a method with the same name in the child class and could access in parent class.
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With