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
32
votes
8 answers

How to make a property protected AND internal in C#?

Here is my shortened abstract class: abstract class Report { protected internal abstract string[] Headers { get; protected set; } } Here is a derived class: class OnlineStatusReport : Report { static string[] headers = new string[] { …
xofz
  • 5,600
  • 6
  • 45
  • 63
32
votes
1 answer

How do I make my controls inside a UserControl private?

I have a user control with a ComboBox and a TextBox. Everything is working great except I noticed that from my user control's instance object, I can access those two controls. They shouldn't be accessible except via my own exposed properties.
ScottG
  • 10,711
  • 25
  • 82
  • 111
30
votes
5 answers

Change the access modifier of an overridden method in Java?

Is there a reason one can change the access modifier of an overridden method? For instance, abstract class Foo{ void start(){...} } And then change the package-private access modifier to public, final class Bar extends Foo{ @Override …
mre
  • 43,520
  • 33
  • 120
  • 170
30
votes
3 answers

Why can I access a derived private member function via a base class pointer to a derived object?

#include using namespace std; class base { public: virtual void add() { cout << "hi"; } }; class derived : public base { private: void add() { cout << "bye"; } }; int main() { base *ptr; ptr = new…
Bruce
  • 33,927
  • 76
  • 174
  • 262
30
votes
13 answers

What does the "private" modifier do?

Considering "private" is the default access modifier for class Members, why is the keyword even needed?
Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
30
votes
4 answers

Default access modifier for a Java constructor

Can anybody explain what the default access modifier is for an explicit no-arg constructor (and other constructors)?
nfc-uk
  • 696
  • 3
  • 7
  • 15
29
votes
4 answers

Error 1 Inconsistent accessibility: return type is less accessible than method

When I'm building, VS show error. This is my code: public Composite buildComposite(ComboBox subs, ComboBox bas) { int count = 0; Composite a = new Composite(); if (subs.SelectedItem != null) { foreach (Substance d in…
user3453838
  • 419
  • 1
  • 6
  • 10
28
votes
5 answers

What is the difference between static, internal and public constructors?

What is the difference between static, internal and public constructors? Why do we need to create all of them together? static xyz() { } public xyz() { } internal xyz() { }
Shankaranarayana
  • 583
  • 1
  • 6
  • 10
27
votes
9 answers

Can non-static methods modify static variables

I am wondering how a non static method can modify a static variable. I know that static methods can only access other static methods and static variables. However, is the other side true? Can non-static methods access only non-static variables? For…
Brian
  • 7,098
  • 15
  • 56
  • 73
26
votes
3 answers

Prevent visual studio from limiting the setter method to internal

Well, I use visual studio 2015 CE, update 2. One productivity hack I usually do is that I create empty model classes like: public class PersonModel { } and then use them in a select expression like: db.People.Where(p => someCondition) .Select(p =>…
Alireza
  • 5,421
  • 5
  • 34
  • 67
26
votes
6 answers

Why have class-level access modifiers instead of object-level?

While using C#, I recently realised that I can call a Foo object's private functions from Foo's static functions, and even from other Foo objects. After everything I have learned about access modifiers, this sounds very strange to me. As far as I…
Lee White
  • 3,649
  • 8
  • 37
  • 62
25
votes
6 answers

"protected" methods in C#?

What are the benefits to defining methods as protected in C#? like : protected void KeyDemo_KeyPress( object sender, KeyPressEventArgs e ) { // some code } As compared to something like this: private void FormName_Click( object sender,…
Sherif
  • 1,249
  • 4
  • 15
  • 38
25
votes
2 answers

Visual Studio's Access Modifier drop down option is disabled for resource file

Not sure why Access Modifier drop down is disabled for a Resource file. alt text http://img683.imageshack.us/img683/9157/accessmodifier.png Here are file properties: alt text http://img199.imageshack.us/img199/3930/resxprop.png
dev.e.loper
  • 35,446
  • 76
  • 161
  • 247
25
votes
8 answers

Private class as return type from public method

Why is this valid? Foo.java public class Foo { public Bar getBar() { return new Bar(); } private class Bar {} } If Bar is private, how will users of this class use this method? Polymorphism can be used of course, but…
Noel De Martin
  • 2,779
  • 4
  • 28
  • 39
24
votes
9 answers

C# private, static, and readonly

I was reviewing some code for log4net and I came across this. private static readonly ILog logger = LogManager.GetLogger(typeof(AdminClient)); I am wondering why would you need to have private static readonly. From my understanding private would…
ant2009
  • 27,094
  • 154
  • 411
  • 609