Blunderfest
Blunderfest

Reputation: 1854

What are the practical difference between declaring a function private vs public?

So as I've been reading/learning about classes and the methods within them I've found very little about the practical differences between declaring a method as public versus private.

I know that the difference is a private class can only be accessed within the class, while a public method can be accessed from code outside the class (other classes, functions). But what I really want to know is:

Also, I don't know if it matters, but I am learning primarily VB.Net and C# in a web application environment, so specifics to that would help.

Upvotes: 5

Views: 10869

Answers (4)

Michael Rodrigues
Michael Rodrigues

Reputation: 5137

Encapsulation means that you should think of each class as a machine that provides a service. For example, a chair allows you to sit on it, or a lawnmower allows you to cut your lawn.

The private methods pertain to the machine's internal workings. In contrast, the public methods relate to how you (other classes) interact with the machine.

Example one: Chair...

When sitting on a chair, you don't need to know volume of stuffing or the number of staples, you basically need to know whether or not it's occupied and if it's stable.

  • Public methods: IsStable, IsOccupied, Sit
  • Private methods: CalculateStuffingVolume, CountNumberOfStaples

Example two: Lawnmower...

For the lawnmower, you need to know if it has enough fuel (or is plugged in), if the blades are sharp, and be able to turn it on.

  • Public methods: GetFuelLevel, IsBladesSharp, TurnOn, TurnOff
  • Private methods: Combust, etc, Too many to imagine.

Conclusion:

So when you're developing all you will see is...

Example one: Chair.Sit, Chair.IsStable and Chair.IsOccupied

or

Example two: Lawnmower.GetFuelLevel, Lawnmower.IsBladesSharp, Lawnmower.TurnOn, LawnMower.TurnOff

As a developer, you will not have to think about number of threads in the uphosltry, the colour of the fuel cap, the number of RPM of the blades or whether the chair is glued or stapled together. This distinction makes it much easier to put your application together without being swamped in detail. Additionally, it allows programmers to expose only necessary information which adds a level of security. As John mentioned, this prevents the Person class from calling Lawnmower.Combust(fuel) when they're not supposed to.

Upvotes: 10

automatix
automatix

Reputation: 14532

It's one of the most importand principles of the object-oriented programming -- encapsulation.

A class usually provides a (public) interface. E.g. (pseudocode):

class Rectangle {
    private length;
    private width;

    public getPerimeter() {
        // return calculatePerimeterOld();
        return calculatePerimeterNew();
    }

    private calculatePerimeterOld() {
        // old variant
    }

    private calculatePerimeterNew() {
        // Here the perimeter is caltulated.
        // so: perimeter = 2 * (length + width)
        // or so: perimeter = 2 * (length) + 2 * (width)
        // or maybe so: perimeter = length + width + length + width - (0.5 * length) + 2 * 0.25 * length)
        return perimeter;
    }

}

I can change my private methods however I want. I can rename or replace them with oher methods. But my public interface will stay the same -- and it has to stay the same, because it's a contract I sign, when I'm defining a mehod as public. Everything, what is signed as private is my "blackbox" and I can do there whatever I want.

It's the main reason. Another reason is, that we should not provide the user(-s) of our class with methods/informations, they don't need. To much (information) is not good.

Upvotes: 2

Derek Tomes
Derek Tomes

Reputation: 4007

If you're developing a one off website maintained just by you, you may find the concept of public and private functions unnecessary.

However, if you're delivering software to other people, and they're building on top of your software, it is an absolutely critical concept.

Think of it like a physical machine that has a thousand knobs and switches inside it, but is contained inside a pretty case with only a few clearly labelled knobs on the outside.

  • The public functions and methods are the ones you create for external parties to interact with your software.
  • The private functions and methods are the ones you create for your software to interact with itself.

But, again, if it's a one-off website maintained by a single developer, these differences are less important.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

If the method is private, then you don't have to think about outside classes calling it incorrectly.

One of the benefits of this is that it allows for a separation between the interface to your class (the public parts of it), and the implementation of it. If nobody knows how you implemented your class (that is, if there is no code that depends on how you implemented it), then you're free to change the way you implemented it without breaking any other code.

Upvotes: 4

Related Questions