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
65
votes
9 answers

Modifier Keyword order in Java

Every time I write a method in Java with more keywords than public void, every time I write it another way. Sometimes "static public void" sometimes "public static void" etc. What is the best order (best practices) for these…
user1227115
  • 843
  • 1
  • 8
  • 11
63
votes
3 answers

Why should constructors on abstract classes be protected, not public?

ReSharper suggests changing the accessibility of a public constructor in an abstract class to protected, but it does not state the rationale behind this. Can you shed some light?
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
63
votes
6 answers

Concept of Private class in C#

Can private classes exist in C#, other than in Inner classes?
Vinod Bhatt
  • 701
  • 1
  • 6
  • 11
61
votes
11 answers

Do constructors always have to be public?

My first question is - class Explain() { public Explain() { } } Should Constructor always declared as public? What if I create a private constructor. I always seen constructors are implicitly public. So why private constructor…
Hash Leon
  • 564
  • 1
  • 5
  • 11
59
votes
4 answers

How does "public private(set)" access modifiers work?

So I'm going through the Apple docs here - Apple Docs Then I ran into this: public struct TrackedString { public private(set) var numberOfEdits = 0 public var value: String = "" { didSet { numberOfEdits += 1 } …
brkr
  • 1,208
  • 1
  • 12
  • 18
53
votes
2 answers

How to show access modifiers for classes in IntelliJ IDEA?

I've recently upgraded to Intellij IDEA 2017.2 and access modifier icons disappeared from my file tree... How to get them back?
mdziob
  • 1,116
  • 13
  • 26
52
votes
7 answers

Internal abstract class: how to hide usage outside assembly?

I have a common assembly/project that has an abstract base class, then several derived classes that I want to make public to other assemblies. I don't want the abstract base class to show up in these other assemblies in Intellisense, so I thought…
m3ntat
  • 3,635
  • 11
  • 38
  • 50
50
votes
5 answers

Why elements defined in a namespace cannot be explicitly declared?

I have the following C# code: namespace ISeeOptic.BL { public abstract class Process { ... protected static void DeleteImages(List list) { some logic } ... } …
Michael
  • 13,950
  • 57
  • 145
  • 288
49
votes
8 answers

Why can't we change access modifier while overriding methods in C#?

In C#, we can not change access modifier while overriding a method from base class. e.g. Class Base { **protected** string foo() { return "Base"; } } Class Derived : Base { **public** override string foo() { return…
Rumit Parakhiya
  • 2,674
  • 3
  • 24
  • 33
48
votes
3 answers

Public, Private - Upper Case, Lower Case:

New to GoLang, coming from Delphi, C++ : First time I tried to create my own package in Go, I followed all the instructions about how to lay out the workspace, etc, but I kept on getting a compiler error: ./myPackage.go:52: undefined: myFunc After…
Vector
  • 10,879
  • 12
  • 61
  • 101
47
votes
8 answers

Is there anything like an Internal class in Java?

In C# you can mark a class as internal so that it is only accessible from within the same package. Is there anything similar in Java?
Svish
  • 152,914
  • 173
  • 462
  • 620
45
votes
1 answer

Why is Unity ignoring the initialized value of a non-static public field?

I'm using InvokeRepeating() to call a method in a game. I call InvokeRepeating() in the Start() method of one of the GameObject classes. To set the repeatRate parameter for InvokeRepeating(), I am passing it a public field called…
user3956566
45
votes
9 answers

What is the difference between access specifiers and access modifiers?

In Java, are access specifiers and access modifiers the same thing?
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
44
votes
7 answers

Why can't I access C# protected members except like this?

This code: abstract class C { protected abstract void F(D d); } class D : C { protected override void F(D d) { } void G(C c) { c.F(this); } } Generates this error: Cannot access protected member 'C.F(D)' via a…
BCS
  • 75,627
  • 68
  • 187
  • 294
43
votes
4 answers

C++ classes (public, private, and protected)

How can classes in C++ be declared public, private, or protected?
Simplicity
  • 47,404
  • 98
  • 256
  • 385
1 2
3
69 70