This question stems from several related questions, and is somewhat reasking the first which doesn't seem to have a clear answer to the OP's main question:
- Private Methods Over Public Methods
- Why hide a class implementation?
- https://softwareengineering.stackexchange.com/questions/132019/what-is-the-value-in-hiding-the-details-through-abstractions-isnt-there-value
I very clearly understand the purpose of private methods, but I am having difficulty thinking of an example where having the function's implementation in a private method wouldn't cause problems for an API consumer, but placing the implementation directly in the public method would. This is a common practice in Java's APIs.
I'm basically re-asking the first question above because I didn't read an answer that solidifies my understanding. Taking the same 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);
}
}
What is the benefit of using the private sum function, rather than just placing the result directly in the public function?
public int getSum(int a, int b) {
return a + b;
}
Perhaps this isn't the best example, but I very much welcome more complex or real-world examples as well.
Again, I understand that placing the implementation in a private method hides the implementation and allows for re-use and is overall a better practice for future additions. However,
- How does not having it in a private method have the ability to disrupt the API's consumers?
- Should all public methods be created this way (always place the implementation in a private method), or where is the line drawn?