Questions tagged [access-modifiers]

Access modifier is an OOP concept. It determines what level of access or visibility a particular property/method/class has.

1041 questions
12
votes
1 answer

What is extension_access_modifier swiftlint?

I added Swiftlint to a project and I'm having trouble understanding what the warning is for extension_access_modifier. I see it mainly on a class that is declared as public, but there are extensions littered throughout the codebase that adds…
Crystal
  • 28,460
  • 62
  • 219
  • 393
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
12
votes
3 answers

How are java.lang.Object's protected methods protected from subclasses?

The keyword protected grants access to classes in the same package and subclasses (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html). Now, every class has java.lang.Object as superclass…
Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
12
votes
1 answer

Why am I able to access private class variables from outside the class, and how can I prevent it?

I am using this code type TSomeClass = class(TOBject) private class var InstanceCount : integer; class var TotalInstanceCount : integer; public class function instances: integer; class function totalInstances: integer; constructor…
Raffaele Rossi
  • 2,997
  • 4
  • 31
  • 62
12
votes
3 answers

Access Modifiers (Private, Protected) in ES6

Note: I already went through the below SO Question and 7 Answers (as of now) about Symbols, WeekMaps and Maps, Please read the full question before you vote: Private properties in JavaScript ES6 classes Article:…
Alamelu Venkat
  • 471
  • 1
  • 4
  • 15
12
votes
4 answers

Java: Expose public class method to all packages of same project but make private for other projects

I have a library project with two packages say package1 and package2 with class1 and class2 respectively. class1 has some public methods exposed to end user. I want to add few utility methods in class1 that only class2 can access. I searched a lot…
Naheed Sultana
  • 354
  • 3
  • 12
12
votes
2 answers

Inconsistent accessibility: return type is less accessible than method C#

Ok, so this is really wierd. I have a private member, and I want to use it into Form2. I've made a public static method, so that I can get that member into Form2. Here is my code: private static AppController appController; private BreadRepository…
icebox19
  • 493
  • 3
  • 6
  • 15
12
votes
1 answer

C++ private modifier ignored on nested anonymous struct

The following sample code compiles just fine in Visual C++: class Test { private: struct { struct { int privateData; }; }; }; int main(int, char **) { Test test; test.privateData = 0; return 0; } But…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
11
votes
6 answers

How to prevent an abstract class with public derived classes from being inherited in other assemblies?

I want to write something like the following: internal class InternalData { } public class PublicData { } abstract internal class Base { internal Base() { } private static InternalData…
penartur
  • 9,792
  • 5
  • 39
  • 50
11
votes
2 answers

C++ access modifier auto indentation in Visual Studio 2010 slowly driving me crazy - can it be changed?

When programming C++ in Visual Studio, it insists on giving me these awful indentations on access modifiers - my condolences if anyone actually likes them this way ;) (a joke folks!) public class MyClass { public: MyClass(); ~MyClass(); int…
Max
  • 4,345
  • 8
  • 38
  • 64
11
votes
3 answers

Visual C# 2010 Express: Specify default access modifier for new classes?

Whenever I create new classes using Visual Studio 2010 Express C# it creates them with no access modifier. 9 times out of 10 I want my new classes to be public. How can I have Visual Studio create empty class templates with the "public" modifier by…
User
  • 62,498
  • 72
  • 186
  • 247
11
votes
4 answers

Static Class vs Protected Constructor

I Am getting a warning message in my class, like Add a Protected constructor or the static keyword to the class declaration Solution The error is gone after I tried both the below ways, static class without constructor public static class Program…
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
11
votes
3 answers

Java: Subclass access without package access

Fairly new to Java, but I'm wondering why package access is considered "more restrictive" than subclass access. That is, every access modifier which provides subclasses with access to a member also provides the whole package with access, and there…
Innominate
  • 121
  • 1
  • 6
11
votes
6 answers

.NET - how to make a class such that only one other specific class can instantiate it?

I'd like to have the following setup: class Descriptor { public string Name { get; private set; } public IList Parameters { get; private set; } // Set to ReadOnlyCollection private Descrtiptor() { } public Descriptor…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
11
votes
3 answers

Which part of JLS said anonymous classes cannot have public/protected/private member classes

Consider this piece of code: public class TopLevelClass { Cloneable c = new Cloneable() { private int privateField; private void privateMethod() {}; }; } There is an anonymous class that has a private member field and a…
johnchen902
  • 9,531
  • 1
  • 27
  • 69