The access-modifiers tag has no usage guidance.
1
vote
1answer
63 views
How to motivate adaptive an shifting class access modifiers
At the new client, we had a discussion regarding access modifiers for classes, methods, members etc. One opinion was to keep things as private as possible, only allowing for protected (internal, ...
237
votes
17answers
18k views
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 being able ...
3
votes
1answer
90 views
Unit testing a large project with few publicly accessible components [duplicate]
I'm asking about C#, but this probably applies to most other languages as well.
Imagine I have a project with a lot of complex logic, split up into a lot of small components. Let's say that, among ...
2
votes
1answer
243 views
Access modifier of abstract class constructors
What access modifier should I use for my constructors in an abstract class, given that the class cannot be instantiated? It seems like the access modifier is superfluous and should be implicitly ...
1
vote
2answers
215 views
How C# protected access modifier when applied to a member variable works
I have 2 classes, Base and child class.
Base class has a protected int variable. My understanding is that any protected member of a class can only be accessed in a child class, It cannot be accessed ...
8
votes
4answers
2k views
Is it bad practice to make methods public solely for the sake of unit testing? [duplicate]
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.
...
-2
votes
2answers
477 views
Why public access level method get overridden in java?
This question is raised with a clarification required to decide when to declare a method protected or public during class design of a package.
My point is, if one needs to override a method of a ...
2
votes
1answer
360 views
Is this JS code a good way for defining class with private methods?
I was recently browsing a open source JavaScript project. The project is a straight port from another project in C language. It mostly use static methods, packed together in classes. Most classes are ...
2
votes
1answer
64 views
Object pointer with privileged access?
Let's say you have
class Car with Car->tune() and Car->drive()
class CarDriver
class CarMechanic
Each CarDriver and CarMechanic object has a reference/pointer to a Car object.
The CarDriver ...
0
votes
3answers
272 views
When to use default access modifier?
I want to understand, from design point of view, in what situations I should use default access modifier, and when I shouldn't.
I found this related question, that provides some basic explanations, ...
15
votes
6answers
6k views
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 ...
4
votes
1answer
142 views
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 ...
1
vote
1answer
737 views
How can you denote the access modifiers “friend” and “protected friend” in UML?
The symbols for access modifiers that I know are relevant for many different languages:
public: +
private: -
...
14
votes
7answers
5k views
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 ...
6
votes
6answers
1k views
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 ...
6
votes
5answers
2k views
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;
}
...
2
votes
3answers
413 views
Want to know performance issues of Class types & access modifiers
Someone suggested me to mark classes as SEALED that are not being inherited anywhere. I was using default style and was not concerned with the type of class. At present I don't have a benchmark tool ...
23
votes
3answers
5k views
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 ...
7
votes
3answers
3k views
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 ...
3
votes
4answers
766 views
How do you remember encapsulation types for effective use?
I've been attempting to learn C#.NET for the past month or so, and the array of ideas that seems to always trip me up is encapsulation. As this is one of the three pillars of OOP, I feel that I am ...
13
votes
6answers
3k views
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 ...