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
24
votes
1 answer

Assigning Typescript constructor parameters

I have interface: export interface IFieldValue { name: string; value: string; } And I have a class that implements it: class Person implements IFieldValue{ name: string; value: string; constructor (name: string, value: string)…
demo
  • 6,038
  • 19
  • 75
  • 149
23
votes
7 answers

multiple classes in a single file : modifier private not allowed here

I am not able to understand why this code doesn't compile: class A { public static void main(String[] args) { System.out.println("hi"); } } private class B { int a; } I am saving the contents in a file named A.java - and I get…
dev
  • 11,071
  • 22
  • 74
  • 122
23
votes
5 answers

Spring @Autowired fields - which access modifier, private or package-private?

Let's say that we use the @Autowired annotation over various fields in a class, and that we didn't write setters or constructors that can also set the fields. Question - what should the access modifier be, private or package-private (i.e. none)…
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
22
votes
5 answers

Detect access modifier type on a property using Reflection

I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection. However I need to find out if the property is public or protected. eg: public string Name{get;set;} protected …
Michael Edwards
  • 6,308
  • 6
  • 44
  • 75
22
votes
1 answer

Equivalent of internal in java

What is equivalent of internal access modifier available in C# for method in Java? (I know default i.e. methods, variables without any scope are having package access but I am looking for keyword equivalent) How can we achieve methods with…
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
22
votes
5 answers

Difference between "strict private" and "protected" Access Modifiers in Delphi?

but I learn programming and after structured programming with Pascal language, I'm beginning to learn about OOP with Delphi. So, I don't really understand the difference between the strict private instruction and the protected one.. So here is my…
bAN
  • 13,375
  • 16
  • 60
  • 93
21
votes
3 answers

Default access modifier for new C#-classes in Visual Studio 2022

When I generate a new class in Visual Studio (C#), I would like to have the default access modifier changed from "internal" to "public". E.g.: public class Animal { } instead of: internal class Animal { } Is there a setting in Visual Studio (2022)…
StOp
  • 313
  • 2
  • 7
21
votes
4 answers

C++ subclassing access modifier?

I'm C++ newbie, and I have many years of experience about OO languages such as C/C#/Objective-C. Now, I'm learning C++. I saw this C++ code: class World : public State { }; It seems class World inherits the class State publicly. Public…
eonil
  • 83,476
  • 81
  • 317
  • 516
21
votes
3 answers

When would you use the "protected internal" access modifier?

As you may already know, the .NET Framework's protected internal access modifier works in a strange way: It doesn't mean the class is protected AND internal, it says the class is protected OR internal; that is, the modified class or member can be…
Pablo Marambio
  • 1,562
  • 1
  • 15
  • 29
20
votes
4 answers

How to access an internal Swift class in Objective-C within the same framework?

Working on a mixed framework. imported inside the Obj-C file but the internal classes are not visible, only the public ones. The documentation clearly states the internal clasees should be available between Swift and Obj-C: Importing Swift into…
Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44
19
votes
5 answers

Ruby private and public accessors

When defining accessors in Ruby, there can be a tension between brevity (which we all love) and best practice. For example, if I wanted to expose a value on an instance but prohibit any external objects from updating it, I could do the…
A Fader Darkly
  • 3,516
  • 1
  • 22
  • 28
18
votes
2 answers

Why is it not "inconsistent accessibility" to use a private nested type inside a generic type in the interface list?

In case the title is not completely self-explanatory, here's the code that puzzles me: public interface IFoo { } public class MyClass : IFoo { private class NestedInMyClass { } } I'm suprised this compiles with no…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
18
votes
3 answers

Private constructor inhibits use of emplace[_back]() to avoid a move

Consider the following code: #include class A { public: A(A&&); // somewhat expensive static std::vector make_As() { std::vector result; result.push_back(A(3)); result.push_back(A(4)); …
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
17
votes
2 answers

Why must a base class destructor be accessible only when a custom constructor is declared?

Comeau, g++ (ideone) and EDG accept the following code without diagnostic. Visual C++ compiles successfully, albeit with warning C4624. class indestructible_base { ~indestructible_base(); }; class T : indestructible_base { public: //T()…
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
17
votes
7 answers

Why C# does not support the intersection of Protected and Internal accessibility?

protected internal: The union of protected and internal accessibility (this is less restrictive than protected or internal alone) The CLR has the concept of intersection of protected and internal accessibility, but C# does not support this. So my…
Omar
  • 16,329
  • 10
  • 48
  • 66