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
136
votes
6 answers

What is the meaning of the planned "private protected" C# access modifier?

As part of the Roslyn documentation on GitHub, there's a page called Language feature implementation status, with planned language features for C# and VB. One feature I couldn't wrap my head around was private protected access modifier: private…
Kobi
  • 135,331
  • 41
  • 252
  • 292
128
votes
10 answers

Any reason to write the "private" keyword in C#?

As far as I know, private is the default everywhere in C# (meaning that if I don't write public, protected, internal, etc. it will be private by default). (Please correct me if I am wrong.) So, what's the reason to write that keyword, or why does it…
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
113
votes
7 answers

What are public, private and protected in object oriented programming?

What are public, private and protected in object oriented programming?
Delirium tremens
  • 4,623
  • 9
  • 46
  • 59
97
votes
7 answers

Isn't "package private" member access synonymous with the default (no-modifier) access?

I am a little confused over the term "package private" that some of the documentation uses, along with the usage of "default access." Aren't package-private and default access both synonymous with protected?
TurtleToes
  • 2,047
  • 2
  • 17
  • 17
92
votes
7 answers

Are private methods really safe?

In Java the private access modifier consider as safe since it is not visible outside of the class. Then outside world doesn't know about that method either. But I thought Java reflection can use to break this rule. Consider following case: public…
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
92
votes
5 answers

Static block in Java not executed

class Test { public static void main(String arg[]) { System.out.println("**MAIN METHOD"); System.out.println(Mno.VAL); // SOP(9090); System.out.println(Mno.VAL + 100); // SOP(9190); } } class Mno { final…
Sthita
  • 1,750
  • 2
  • 19
  • 38
87
votes
13 answers

Class variables: public access read-only, but private access read/write

Whoopee, not working on that socket library for the moment. I'm trying to educate myself a little more in C++. With classes, is there a way to make a variable read-only to the public, but read+write when accessed privately? e.g. something like…
FurryHead
  • 1,479
  • 3
  • 16
  • 19
87
votes
15 answers

Why can't I have protected interface members?

What is the argument against declaring protected-access members on interfaces? This, for example, is invalid: public interface IOrange { public OrangePeel Peel { get; } protected OrangePips Seeds { get; } } In this example, the interface…
ajlane
  • 1,899
  • 1
  • 18
  • 23
84
votes
5 answers

Public and Internal members in an Internal class?

Ok, so this may be a bit of a silly question, and there's certainly the obvious answer, but I was curious if I've missed any subtleties here. Is there any difference in terms of visibility/usability between a public member declared in an internal…
Noldorin
  • 144,213
  • 56
  • 264
  • 302
80
votes
8 answers

When overriding a method, why can I increase access but not decrease it?

Why does Java specify that the access specifier for an overriding method can allow more, but not less, access than the overridden method? For example, a protected instance method in the superclass can be made public, but not private, in the…
yesilupper
  • 813
  • 1
  • 7
  • 7
80
votes
4 answers

What is the use case for the (C# 7.2) "private protected" modifier?

C# 7.2 introduces the private protected modifier. I've always protected access to fields with properties, allowing access via the Get/Set methods as I typically don't want the internal state of my object modified by anything other than my own…
Jay
  • 9,561
  • 7
  • 51
  • 72
75
votes
1 answer

What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

In VB6/VBA, you can declare module-level variables outside of a specific Sub or Function method. I've used Private and Public before inside modules and understand them like so: Public - visible to all code inside the module and all code outside…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
67
votes
3 answers

Difference between Private Sub, Function and Class

What are the differences between the following: Private Sub Private Function Private Class When should each one be used?
Furqan Sehgal
  • 4,917
  • 33
  • 108
  • 167
67
votes
2 answers

What are the differences between final class and sealed class in Scala?

There are two types of modifiers in Scala: final and sealed What are the differences between them? When should you use one over the other?
user1187135
65
votes
1 answer

What does 'fileprivate' keyword means in Swift?

I'm starting in swift and opening a project created using swift2 from xcode 8 beta, the private modifier were changed to fileprivate. what does this keyword means? and how is different from private ?
Nathaniel
  • 952
  • 1
  • 7
  • 12
1
2
3
69 70