Questions tagged [access-modifiers]

Access modifiers control where variables can be accessed. The common ones are public, protected, and private. Use this tag for access issues caused by access modifiers.

41 questions
276
votes
17 answers

Why have private fields, isn't protected enough?

Is the visibility private of class fields/properties/attributes useful? In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and be able to modify the implementation completely. One of the…
Adam Libuša
  • 2,067
  • 2
  • 10
  • 14
103
votes
8 answers

Why have private static methods?

I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility? I would have thought an advantage to having a static method is that it can be called without an…
41
votes
8 answers

Do access modifiers matter?

The theory is that access modifiers improve code safety because they support encapsulation of internal state. When doing OOP, every language I've used implements some kind of access restriction. I like some access models better than others. I am on…
ahoffer
  • 613
  • 7
  • 19
33
votes
4 answers

Is it bad practice to make methods public solely for the sake of unit testing?

I have a class with a public method. It has other methods that 'assist' with the purpose of the public method. The public method must be tested. However, I also want to unit test the private methods. It would be impossible for my unit tests to call…
JᴀʏMᴇᴇ
  • 2,361
  • 4
  • 22
  • 25
27
votes
4 answers

Why aren't there explicit access modifiers in Python:

If 'explicit is better than implicit', why aren't there explicit access modifiers in Python: Public, Protected, Private, etc.? I know that the idea is that the programmer should know what to do through a hint - no need to use 'brute force'. But IMO…
Vector
  • 3,180
  • 3
  • 22
  • 25
27
votes
7 answers

Why did Java make package access default?

I'm asking this question because I believe they did it for a very good reason and that most people do not use it properly, well from my experience in industry so far anyway. But if my theory is true then I'm not sure why they included the private…
26
votes
5 answers

Why did it not become a common pattern to use setters in the constructor?

Accessors and modifiers (aka setters and getters) are useful for three main reasons: They restrict access to the variables. For example, a variable could be accessed, but not modified. They validate the parameters. They may cause some side…
Allan Spreys
  • 845
  • 1
  • 8
  • 13
16
votes
6 answers

Real-world scenarios for protected methods

Today I noticed that I basically never use protected methods in C++ code, because I rarely feel the need to call non-public methods of a parent. I do use protected in Java in the template method pattern, but since you can override private methods in…
fredoverflow
  • 6,854
  • 8
  • 39
  • 46
16
votes
7 answers

In Java, why were protected members made accessible to classes of the same package?

From the official documentation... Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N …
jramoyo
  • 610
  • 5
  • 13
8
votes
3 answers

Should I use default access modifier or not -- Coding practice?

Normally when creating new global variables I do not define its access modifier. So as per java it will adopt property default access modifier. When I'm need to access that variable in out of default scope I change its access modifier else leave it…
Harry Joy
  • 2,197
  • 2
  • 18
  • 20
8
votes
4 answers

Are there any legitimate use cases for protected visibility?

Protected visibility in languages like C++, Java or PHP is a strange beast: it makes fields and methods accessible in subclasses, but not in code completely outside the class. It strikes me as somewhat arbitrary to allow subclasses to access a class…
6
votes
6 answers

Organizing Class Members in Regards to Access Modifier

If we look at typical implementation of a Class, we usually see the private members defined at the beginning and public( mostly functions and Accessors) defined towards the bottom. Now, is this a Industry standard agreed by a lot of people? How…
6
votes
7 answers

Using a SetProperty method to prevent accidental changes to a property

Is it good/bad practice to do the following: public class MyClass { public MyType MyProperty { get; private set; } public void SetMyProperty(MyType myProperty) { MyProperty = myProperty; } } My intention is to prevent MyProperty from…
James
  • 345
  • 1
  • 3
  • 11
5
votes
1 answer

Why is accessing virtual protected functions of a base class not allowed through a pointer of the base class type inside a derived class

Consider the following: #include class Base { public: void go(){this->doSomething();} protected: virtual void doSomething(){std::cout << "base";} }; class DerivedA : public Base { protected: void…
Andy Forrest
  • 61
  • 1
  • 1
  • 3
5
votes
1 answer

Is avoiding the private access specifier in PHP justified?

I come from a Java background and I have been working with PHP for almost a year now. I have worked with WordPress, Zend and currently I'm using CakePHP. I was going through Cake's lib and I couldn't help notice that Cake goes a long way avoiding…
1
2 3