Private methods over public methods

I studied the StringTokenizer.java class, and there were a few questions that came to mind.

I noticed that public methods that should be used by other classes called some kind of private method that did all the work. Now I know that one of the principles of OOD is that you can do as you can and hide all implementation details. I'm not sure I fully understand the logic behind this.

I understand that it is important to close the fields to prevent invalid values โ€‹โ€‹in them (only one of many reasons). However, when it comes to private methods, I'm not sure why they are just as important.

For example, in the case of the StringTokenizer class, StringTokenizer we just put all the implementation code in public methods? How would this affect the classes that use these methods, since the API for these methods (i.e., the Rules for calling these public methods) would remain the same? The only reason I can think about why private methods are useful is because it helps you write duplicate code. For example, if all public methods did the same, then you can declare a private method that performs this task and that can be used by public methods.

Another question: what is the advantage of writing an implementation in a private method as opposed to a public method?

Here is a small example:

 public class Sum{ private int sum(int a, int b){ return a+b; } public int getSum(int a, int b){ return sum(a,b); } } 

Vs ...

 public class Sum{ public int getSum(int a, int b){ return a+b; } } 

How is the first example more useful?

+24
java encapsulation stringtokenizer public-method
Oct 13 '11 at 3:12
source share
7 answers

To add something, a private method can ALWAYS be changed safely, because you know that you only call from your own class, no external classes can call a private method (they donโ€™t even see it).

Thus, the presence of a private method is always good, since you know that there are no problems with changing it, even you can safely add additional parameters to this parameter.

Now think of a public method, anyone can call this method, so if you add / remove a parameter, you will need to change ALL calls to this method as well.

+28
Oct 13 2018-11-11T00:
source share

The only reason I can think about why private methods are useful is because it helps you write duplicate code.

In addition to consolidating repeating code (often expressed as โ€œDon't repeat yourselfโ€ or โ€œDRYโ€), using private methods can also help you structure and document your code. If you find yourself writing a method that does several things, you might consider splitting it into several private methods. This can make it clearer what inputs and outputs are for each part of the logic (with finer granularity). In addition, descriptive method names can help complement code documentation.

+2
Oct 13 '11 at 3:20
source share

When writing clean code in Java or any other object-oriented language, in general, the cleanest, most readable code consists of short, concise methods. The question often arises that the logic in a method can be better expressed in separate method calls in order to make the code cleaner and more convenient for maintenance.

With this in mind, we can imagine situations when you have many methods that perform tasks for one purpose. Think of a class that has only one challenging goal. An entry point for this single purpose may require only one starting point (one open method), but many other methods that are part of a complex operation (many private help methods).

Using private methods, we can hide logic that is not and should not be accessible anywhere outside the class itself.

+1
Oct 13 2018-11-11T00:
source share

Public methods are usually code that other classes that implement this class will want to use. Private methods, as a rule, are not so useful outside the classroom, or they do not (alone) serve the purpose of what the class should do.

Suppose you are in your IDE and you are implementing some kind of class A. Class A is intended only to do one thing, say, to generate a document. Naturally, you will have some mathematical and byte methods of working in class A that are needed to create a document, but people trying to use class A will not need these other methods because they just want a document. Therefore, we make these methods confidential, so that everything is easier for any future users of our class.

+1
Oct 13 2018-11-11T00:
source share

The purpose of declaring a private method is

  • hide implementation details
  • remove a method from the list as an open API
  • make sure the code logic is not used / not used externally
  • in most cases, the execution of your method depends on other methods that are executed before it; then you can also be sure that you are in control of the correct sequence of using your method.

Use private for your methods if you do not intend to use your method outside the context of your class.

+1
Jun 10 '16 at 2:03
source share

Performing private functions gives you an advantage in the following cases:

  • Creating a private function gives the JVM compiler the ability to embed the function and, therefore, increase application performance.
  • If the class is inherited and you extend it from the child class, then in case you want to hide functions from the child class, you can do this (you can extend the StringTokenizer).
  • If a code snippet should be used in several functions, you move this code to a private utility method
0
Oct 13 2018-11-11T00:
source share

The advantage, as well as the good reason to use private methods within public classes, is to protect and prevent errors. Methods declared as private are available only to the class of which they are a part. This means that your private method cannot be accidentally called from another place where errors and other complications are reduced in the program. If you declare your method publicly available, the whole problem can be addressed and can cause complications.

You may have several methods that work on a specific piece of data that you do not want any other part of the program to intervene. Using data encapsulation using private methods and / or variables helps prevent this, and also makes it easy to track and document your code.

0
Feb 05 '13 at 12:27
source share



All Articles