Access level modifiers determine whether other classes can use a particular field or invoke a particular method.
Questions tagged [access-levels]
85 questions
75
votes
7 answers
How to restrict access to nested class member to enclosing class?
Is it possible to specify that members of a nested class can be accessed by the enclosing class, but not other classes ?
Here's an illustration of the problem (of course my actual code is a bit more complex...) :
public class Journal
{
public…

Thomas Levesque
- 286,951
- 70
- 623
- 758
39
votes
6 answers
Are protected members/fields really that bad?
Now if you read the naming conventions in the MSDN for C# you will notice that it states that properties are always preferred over public and protected fields. I have even been told by some people that you should never use public or protected…

ryanzec
- 27,284
- 38
- 112
- 169
36
votes
3 answers
Why can't extensions with protocol conformances have a specific access level?
Assume we have the following example code:
protocol MyProtocol {
func someFunction()
}
public class MyClass {
}
public extension MyClass: MyProtocol {
func someFunction() {
print("hello")
}
}
Compiling the code above gives…

Paolo
- 3,825
- 4
- 25
- 41
17
votes
9 answers
Common CMS roles and access levels
I am currently writing a CMS and remember someone (it might have been on here) criticise the existing CMS for not having a robust enough user permissions system. I've got a method planned out but it I feel it has fallen into the usual trap of being…

Meep3D
- 3,803
- 4
- 36
- 55
16
votes
3 answers
Difference between fileprivate and private extension?
Swift 3.0
I know that fileprivate access level modifier limited using of function/property to source file where it was declared and private - limited to lexical scope where was declared. But it seems that this rule not apply for extensions. E.G.…

Bohdan Savych
- 3,310
- 4
- 28
- 47
14
votes
1 answer
"temporary of type 'A' has protected destructor", but its type is B
In the following code, compiled with Clang 8.0.0+ and -std=c++17, creating a derived class instance using B{} gives an error error: temporary of type 'A' has protected destructor. Why is A appearing in this message when the temporary has type B (and…

jtbandes
- 115,675
- 35
- 233
- 266
12
votes
7 answers
What is the difference between private and fileprivate in Swift 4
In Swift 4, since now private is visible in extensions also in the same source code file, how is it different from the fileprivate access modifier?
Background: In Swift 3, private variables in a class are not visible in its extensions in the same…

crypt
- 449
- 5
- 15
8
votes
5 answers
What do you choose, protected or internal?
If I have a class with a method I want protected and internal. I want that only derived classes in the assembly would be able to call it.
Since protected internal means protected or internal, you have to make a choice. What do you choose in this…

brickner
- 6,595
- 3
- 41
- 54
7
votes
2 answers
How to hide a protected procedure of an object?
In one base class, there's a protected procedure. When inheriting that class, I want to hide that procedure from being used from the outside. I tried overriding it from within the private and even strict private sections, but it can still be called…

Jerry Dodge
- 26,858
- 31
- 155
- 327
7
votes
3 answers
Is there a language with object-based access levels?
A common misconception about access level in Java, C#, C++ and PHP is that it applies to objects rather than classes. That is, that (say) an object of class X can't see another X's private members. In fact, of course, access level is class-based…

Carl Manaster
- 39,912
- 17
- 102
- 155
6
votes
2 answers
Java define a explicit package-private modifier
Obviously Java has a Access level package-private which achieved by not adding any explicit modifier.
But isn't there a way to explicitly add this modifier? It's a bit confusing that we need to omit access level when we want to use member in package…

Ori Marko
- 56,308
- 23
- 131
- 233
6
votes
2 answers
Why do I get an "Access Level must be protected or weaker" after extending a protected class variable and marking it private?
abstract class AbstractController
{
protected $repository;
}
class GraphController extends AbstractController
{
private $repository;
}
I get this error:
Fatal error: Access level to GraphController::$repository must be protected or weaker…

Dennis
- 7,907
- 11
- 65
- 115
5
votes
2 answers
Rails authlogic : How to make Levels?
I followed this tutorial for setting Autlogic up properly. So, my site needs a form of level, like "Admin", "Moderator", "User", "Guest". So Admins can do everything, where Moderators may not can make site changes. And Users can't destroy, Update or…

Oluf Nielsen
- 55
- 3
5
votes
4 answers
access exception when invoking method of an anonymous class using java reflection
I'm trying to use an event dispatcher to allow a model to notify subscribed listeners when it changes. the event dispatcher receives a handler class and a method name to call during dispatch. the presenter subscribes to the model changes and provide…

Asaf David
- 3,583
- 3
- 29
- 33
5
votes
2 answers
Protection Levels and Visual Studio unit tests with C#
I am setting up some unit tests for a C# project I'm working on, and I opted to use Visual Studio's built-in unit test project. The problem is that I have been giving most of my classes in the project the default, internal access level. Now they…

Matt Kline
- 10,149
- 7
- 50
- 87