Maam please explain why are we taking two function in which one is private and other is public for all functions we have created for height,preorder etc.
public void preOrder() {
this.preOrder(this.root);
}
private void preOrder(Node node) {
}
Maam please explain why are we taking two function in which one is private and other is public for all functions we have created for height,preorder etc.
public void preOrder() {
this.preOrder(this.root);
}
private void preOrder(Node node) {
}
In your simple example, there is not really a reason for privateMethod
to exist because all publicMethod
does is call it, but in a more real-world example, the private methods would do things that you don’t want other modules calling on their own.So its a good practice to do so .
Also In order to add something, a private method can ALWAYS be changed safely, because you know for sure that is called only from the own class, no external classes are able to call a private method (they can’t even see it).
So having a private method is always good as you know there is no problem about changing it, even you can safely add more parameters to the method.
Now think of a public method, anyone could call that method, so if you add/remove a parameter, you will need to change also ALL the calls to that method