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
2 votes
2 answers
375 views

Can access modifiers be completely replaced with programming to interfaces?

If we program to interfaces various parts of the implementation can be effectively hidden. We can define multiple interfaces for a single implementation and use them as needed, instead of 4 fixed ...
Ondrej Bozek's user avatar
8 votes
4 answers
914 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 ...
user3840170's user avatar
3 votes
5 answers
2k 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 ...
Evorlor's user avatar
  • 1,442
4 votes
1 answer
527 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 ...
Andrew Williamson's user avatar
41 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 ...
ahoffer's user avatar
  • 613
2 votes
1 answer
2k 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 ...
xoric's 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?...
aCarella's user avatar
  • 329
2 votes
3 answers
503 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 ...
Konrad's user avatar
  • 1,529
2 votes
6 answers
3k 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 ...
5argon's user avatar
  • 129
1 vote
1 answer
711 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 ...
h22's user avatar
  • 917
2 votes
0 answers
118 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 ...
Umer Amjad's user avatar
1 vote
3 answers
4k 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 ...
Gil Sand's user avatar
  • 2,173
1 vote
0 answers
314 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 ...
Adam Arold's user avatar
  • 1,180
2 votes
2 answers
151 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 ...
Winter's 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 ...
Ferenc Dajka's user avatar
1 vote
2 answers
1k 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() {.....
maaartinus's user avatar
  • 2,633
5 votes
1 answer
10k 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";} }...
Andy Forrest's user avatar
26 votes
5 answers
23k 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 ...
Allan Spreys's user avatar
3 votes
3 answers
3k 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; } ...
FLSH's user avatar
  • 159
1 vote
1 answer
98 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, ...
Konrad Viltersten's user avatar
276 votes
17 answers
26k 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 ...
Adam Libuša's user avatar
  • 2,067
3 votes
1 answer
279 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 ...
user622505's 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 ...
rory.ap's user avatar
  • 879
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 ...
Charu's user avatar
  • 113
33 votes
4 answers
42k 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. ...
JᴀʏMᴇᴇ's 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 ...
overexchange's user avatar
  • 2,245
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 ...
tigrou's user avatar
  • 269
2 votes
1 answer
124 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 ...
donquixote's user avatar
0 votes
3 answers
4k 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, ...
Ilya Gazman's user avatar
105 votes
8 answers
161k 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 ...
Rehan Naqvi's user avatar
  • 1,169
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 ...
newlogic's user avatar
  • 415
5 votes
1 answer
198 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 ...
Tafadzwa Gonera's user avatar
1 vote
1 answer
3k 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 ...
jramoyo's user avatar
  • 610
6 votes
6 answers
5k 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 ...
Shamim Hafiz - MSFT's 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; } ...
James's user avatar
  • 345
4 votes
3 answers
967 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 ...
RPK's user avatar
  • 4,378
27 votes
4 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 ...
Vector's user avatar
  • 3,180
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 ...
Harry Joy's user avatar
  • 2,187
3 votes
4 answers
973 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 ...
Nathanus's 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 ...
fredoverflow's user avatar
  • 6,854