Questions tagged [access-levels]

Access level modifiers determine whether other classes can use a particular field or invoke a particular method.

77 questions
70
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
  • 270,447
  • 59
  • 580
  • 726
34
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
  • 25,398
  • 36
  • 107
  • 160
29
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,404
  • 3
  • 22
  • 39
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,744
  • 4
  • 32
  • 55
11
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
  • 104,858
  • 34
  • 217
  • 242
11
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
  • 309
  • 4
  • 13
11
votes
2 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
  • 2,442
  • 3
  • 21
  • 42
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,367
  • 1
  • 37
  • 52
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
  • 25,720
  • 28
  • 139
  • 301
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
  • 38,312
  • 15
  • 96
  • 147
5
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
  • 6,954
  • 8
  • 53
  • 97
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…
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,333
  • 3
  • 24
  • 33
5
votes
8 answers

C# protected field to private, add property--why?

In Visual Studio 2008 Team System, I just ran Code Analysis (from the Analyze menu) on one of my C# projects. One of the warnings produced was the following: Microsoft.Design : Because field 'Connection._domain' is visible outside of its declaring…
Sarah Vessels
  • 27,994
  • 29
  • 147
  • 217
4
votes
1 answer

Swift: Calling function from a different target

I have two targets (Target A, Target B) and from Target B I would like to call a function which is located in Target A. I get the Use of Unresolved Identifier error when trying to compile as expected since ClassA.swift isn't part of Target B. So I…
1
2 3 4 5 6