Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the amount of visibility on methods and attributes important?

Why shouldn't one leave all methods and attributes accessible from anywhere (i.e. public)?

Can you give me an example of a problem I can run into if I declared an attribute as public?

like image 741
ziiweb Avatar asked Sep 12 '11 17:09

ziiweb


People also ask

What is meant by public visibility of a method?

A member method of a class declared with private access specifier is said to have private visibility. Only other member methods of its class can call this method.

What refers to visibility of methods or properties of a class?

Visibility ¶ The visibility of a property, a method or (as of PHP 7.1. 0) a constant can be defined by prefixing the declaration with the keywords public , protected or private . Class members declared public can be accessed everywhere.

What is method and attribute?

Any variable that is bound in a class is a class attribute . Any function defined within a class is a method . Methods receive an instance of the class, conventionally called self , as the first argument.

What's the default visibility of methods and fields in a Java class?

Java has four levels of visibility: public, protected, (default), private. The meaning of these is as follows: public - makes your methods accessible to any other class. protected - makes your methods accessible to any class in the same package OR any subclass of your class.


2 Answers

Think of McDonald's as an object. There's a well known public method to order a BigMac.

Internally there's going to be a few zillion other calls to actually GET the materials for making that Bigmac. They don't want you to know how their supply chain works, so all you get is the public Gimme_a_BigMac() call, and would never ever allow you to get access to the Slaughter_a_cow() or Buy_potatoes_for_fries() methods.

For your own code, that no one will ever see, go ahead and leave everything public. But if you're doing a library for others to reuse, then you go and protect the internal details. That leaves McDonald's free to switch to having Scotty beam over a patty rather than having to call up a Trucking company to deliver the meat by land. The end-user never knows the difference - they just get their BigMac. But internally everything could fundamentally change.

like image 144
Marc B Avatar answered Oct 07 '22 19:10

Marc B


Why shouldn't one leave all methods and attributes accessible from anywhere (i.e. public)?

Because that is far too expensive.

Every public method that I make has to be carefully designed and then approved by a team of architects, it has to be implemented to be robust in the face of arbitrarily hostile or buggy callers, it has to be fully tested, all problems found during testing have to have regression suites added, the method has to be documented, the documentation has to be translated into at least twelve different languages.

The biggest cost of all though is: the method has to be maintained, unchanged, forever and ever, amen. If I decide in the next version that I didn't like what that method did, I can't change it because customers now rely on it. Breaking backwards compatibility of a public method imposes costs on users and I am loathe to do that. Living with a bad design or implementation of a public method imposes high costs on the designers, testers and implementers of the next version.

A public method can easily cost thousands or even tens of thousands of dollars. Make a hundred of them in a class and that's a million dollar class right there.

Private methods have none of those costs. Spend shareholder money wisely; make everything private that you possibly can.

like image 33
Eric Lippert Avatar answered Oct 07 '22 18:10

Eric Lippert