Questions tagged [access-modifiers]

Access modifiers control where variables can be accessed. The common ones are public, protected, and private. Use this tag for access issues caused by access modifiers.

Filter by
Sorted by
Tagged with
4 votes
3 answers
442 views

Are there any legitimate use cases for protected visibility?

Protected visibility in languages like C++, Java or PHP is a strange beast: it makes fields and methods accessible in subclasses, but not in code completely outside the class. It strikes me as ...
user avatar
3 votes
5 answers
939 views

Am I understanding the new C# naming conventions right?

A couple months ago, Microsoft updated their C# Naming Conventions (https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions). As the developers of C#, I consider ...
user avatar
  • 1,406
4 votes
1 answer
420 views

Design decisions behind access modifiers in C#

I'm currently writing a small language of my own, and have been considering the difference between the C++ style, where the access modifier applies to a block of members, and the C#/Java style, where ...
user avatar
40 votes
8 answers
5k views

Do access modifiers matter? [closed]

The theory is that access modifiers improve code safety because they support encapsulation of internal state. When doing OOP, every language I've used implements some kind of access restriction. I ...
user avatar
  • 603
2 votes
1 answer
1k views

UML - Changing the visibility of operations when overriding them

I am trying to find out if it is allowed in the UML to change the visibility (access modifier) of an operation when overriding it. For example, in Java it is possible to increase the visibility of an ...
user avatar
  • 51
0 votes
1 answer
1k views

Class structure: How should a binary tree and it's node class be implemented in Java in terms of class files?

In implementing a binary tree in Java, should the node class be a separate class file independent of the BinaryTreeclass, or should it be a default class in the same class file as the BinaryTree class?...
user avatar
  • 329
1 vote
3 answers
405 views

Does access modifier of types matter in projects other than class libraries?

I know it matters for class libraries, but does it matter in projects that are not class libraries, e.g. console applications or GUI applications? What's the difference if I use public or internal or ...
user avatar
  • 1,401
2 votes
6 answers
2k views

When to sensibly use public variable in an OOP language?

After I learned to properly use private protected abstract sealed in a language like C# I found no reason to make a variable public ever again. An interface to the variable is usually a public method ...
user avatar
  • 129
1 vote
1 answer
479 views

Need to call private method in third party library. Copy all class or use reflection?

I need to call a method that is available in a third party library, but it is a private method. There is no direct or indirect way to obtain the same functionality via public methods available. I ...
user avatar
  • 689
2 votes
0 answers
113 views

Access modifiers in combination of interpreter pattern with template method pattern

Motivation: (Skip to "The Problem" if you don't need motivation for it) As a project for myself, I'm writing an expression parser for certain kinds of mathematical expressions, and I'm using the ...
user avatar
1 vote
3 answers
3k views

Are protected properties evil? [duplicate]

Introductory piece of code: class BaseClass { protected Foo MyFoo { get; } } class ChildClass : BaseClass { void SomeMethod() { MyFoo.DoStuff(); //Here, I have no idea ...
user avatar
  • 2,113
1 vote
0 answers
263 views

How to create an "internal" api for my library which is not exposed to the user?

Note that my question is not platform specific so that's why I don't add the [kotlin] tag (the code is in Kotlin). If there is a Kotlin-specific answer I'd like to hear about it though I am working ...
user avatar
  • 1,180
2 votes
2 answers
148 views

Implementation wise fields per object in a class composing a collection of objects

Context When implementing a class, there's a distinction between fields that are there because they are part of what the object is and fields that are there because they are needed to implement the ...
user avatar
  • 705
0 votes
1 answer
55 views

Debug class responsibility, accessability

So I'm writing a Flocking System (http://www.red3d.com/cwr/boids/). Each boid calculates it's vectors, I would like to draw these vectors with a helper class for debugging purposes. Who should ...
user avatar
1 vote
2 answers
928 views

Private or default visibility for "file-private" members?

I have a class like this public final class Outer { static final class Inner { /* Used in the package. */ void f() {...} /* Used in Outer. */ WHATEVER void g() {.....
user avatar
  • 2,585
5 votes
1 answer
9k views

Why is accessing virtual protected functions of a base class not allowed through a pointer of the base class type inside a derived class

Consider the following: #include <iostream> class Base { public: void go(){this->doSomething();} protected: virtual void doSomething(){std::cout << "base";} }...
user avatar
24 votes
5 answers
21k views

Why did it not become a common pattern to use setters in the constructor?

Accessors and modifiers (aka setters and getters) are useful for three main reasons: They restrict access to the variables. For example, a variable could be accessed, but not modified. They ...
user avatar
3 votes
3 answers
2k views

Testing properties with private setters

Currently in a part of my project a domain object like below exists: public class Address { public virtual string HouseName { get; set; } public virtual string HouseNumber { get; set; } ...
user avatar
  • 159
1 vote
1 answer
97 views

How to motivate adaptive an shifting class access modifiers

At the new client, we had a discussion regarding access modifiers for classes, methods, members etc. One opinion was to keep things as private as possible, only allowing for protected (internal, ...
user avatar
273 votes
17 answers
25k views

Why have private fields, isn't protected enough?

Is the visibility private of class fields/properties/attributes useful? In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and be able to ...
user avatar
  • 2,027
3 votes
1 answer
246 views

Unit testing a large project with few publicly accessible components [duplicate]

I'm asking about C#, but this probably applies to most other languages as well. Imagine I have a project with a lot of complex logic, split up into a lot of small components. Let's say that, among ...
user avatar
2 votes
1 answer
2k views

Access modifier of abstract class constructors

What access modifier should I use for my constructors in an abstract class, given that the class cannot be instantiated? It seems like the access modifier is superfluous and should be implicitly ...
user avatar
  • 859
1 vote
2 answers
2k views

How C# protected access modifier when applied to a member variable works

I have 2 classes, Base and child class. Base class has a protected int variable. My understanding is that any protected member of a class can only be accessed in a child class, It cannot be accessed ...
user avatar
  • 113
31 votes
4 answers
39k views

Is it bad practice to make methods public solely for the sake of unit testing? [duplicate]

I have a class with a public method. It has other methods that 'assist' with the purpose of the public method. The public method must be tested. However, I also want to unit test the private methods. ...
user avatar
-2 votes
2 answers
1k views

Why public access level method get overridden in java?

This question is raised with a clarification required to decide when to declare a method protected or public during class design of a package. My point is, if one needs to override a method of a ...
user avatar
  • 2,225
2 votes
1 answer
571 views

Is this JS code a good way for defining class with private methods?

I was recently browsing a open source JavaScript project. The project is a straight port from another project in C language. It mostly use static methods, packed together in classes. Most classes are ...
user avatar
  • 259
2 votes
1 answer
111 views

Object pointer with privileged access?

Let's say you have class Car with Car->tune() and Car->drive() class CarDriver class CarMechanic Each CarDriver and CarMechanic object has a reference/pointer to a Car object. The CarDriver object ...
user avatar
0 votes
3 answers
3k views

When to use default access modifier?

I want to understand, from design point of view, in what situations I should use default access modifier, and when I shouldn't. I found this related question, that provides some basic explanations, ...
user avatar
93 votes
8 answers
137k views

Why have private static methods?

I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility? I would have thought an advantage to having a ...
user avatar
  • 1,049
27 votes
7 answers
18k views

Why did Java make package access default?

I'm asking this question because I believe they did it for a very good reason and that most people do not use it properly, well from my experience in industry so far anyway. But if my theory is true ...
user avatar
  • 415
5 votes
1 answer
185 views

Is avoiding the private access specifier in PHP justified?

I come from a Java background and I have been working with PHP for almost a year now. I have worked with WordPress, Zend and currently I'm using CakePHP. I was going through Cake's lib and I couldn't ...
user avatar
1 vote
1 answer
2k views

How can you denote the access modifiers "friend" and "protected friend" in UML?

The symbols for access modifiers that I know are relevant for many different languages: public:        + private:       - ...
user avatar
16 votes
7 answers
9k views

In Java, why were protected members made accessible to classes of the same package?

From the official documentation... Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N ...
user avatar
  • 610
6 votes
6 answers
4k views

Organizing Class Members in Regards to Access Modifier

If we look at typical implementation of a Class, we usually see the private members defined at the beginning and public( mostly functions and Accessors) defined towards the bottom. Now, is this a ...
user avatar
6 votes
7 answers
9k views

Using a SetProperty method to prevent accidental changes to a property

Is it good/bad practice to do the following: public class MyClass { public MyType MyProperty { get; private set; } public void SetMyProperty(MyType myProperty) { MyProperty = myProperty; } ...
user avatar
  • 335
4 votes
3 answers
856 views

Want to know performance issues of Class types & access modifiers

Someone suggested me to mark classes as SEALED that are not being inherited anywhere. I was using default style and was not concerned with the type of class. At present I don't have a benchmark tool ...
user avatar
  • 4,348
28 votes
3 answers
11k views

Why aren't there explicit access modifiers in Python:

If 'explicit is better than implicit', why aren't there explicit access modifiers in Python: Public, Protected, Private, etc.? I know that the idea is that the programmer should know what to do ...
user avatar
  • 3,164
8 votes
3 answers
7k views

Should I use default access modifier or not -- Coding practice?

Normally when creating new global variables I do not define its access modifier. So as per java it will adopt property default access modifier. When I'm need to access that variable in out of default ...
user avatar
  • 2,147
3 votes
4 answers
950 views

How do you remember encapsulation types for effective use?

I've been attempting to learn C#.NET for the past month or so, and the array of ideas that seems to always trip me up is encapsulation. As this is one of the three pillars of OOP, I feel that I am ...
user avatar
  • 243
16 votes
6 answers
5k views

Real-world scenarios for protected methods

Today I noticed that I basically never use protected methods in C++ code, because I rarely feel the need to call non-public methods of a parent. I do use protected in Java in the template method ...
user avatar
  • 6,834