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
14
votes
2 answers

In Python, how do I write unit tests that can access private attributes without exposing them?

I am trying to improve how I write my unit test cases for my Python programs. I am noticing in some cases, it would be really helpful to have access to private members to ensure that a method is functioning properly. An example case would be when…
grg-n-sox
  • 717
  • 2
  • 5
  • 25
14
votes
1 answer

Can't use public nested class as private method parameter

In the following code: class Outer { private: void f_private(Outer::Inner in); // Wrong public: class Inner {}; void f_public(Outer::Inner in); // OK }; f_private() cannot use nested class Outer::Inner as parameter type. But it's…
Neo
  • 1,031
  • 2
  • 11
  • 27
14
votes
1 answer

Difference between private protected and internal protected

C# 7.2 introduced the private protected modifier, whats the difference to internal protected? From the doc: A private protected member is accessible by types derived from the containing class, but only within its containing assembly. Isn't that…
Enes Sadık Özbek
  • 993
  • 10
  • 17
14
votes
6 answers

Restricting parent class members to its immediate child class only

Suppose I have four classes in java with given hierarchy. class A {} class B extends A {} class C extends B {} class D extends C {} As per my understanding all the accessible fields of class A will be available to all its child classes through…
user8001621
  • 149
  • 1
  • 5
14
votes
3 answers

Swift Public protocols with Internal functions and properties

I am wondering what the best practice is when I want some functions to be public and some to me internal when working with protocols.I am writing an AudioManager in Swift 3 wrapping AVPlayer as a framework. I want some methods to be public, so that…
Sajjon
  • 8,938
  • 5
  • 60
  • 94
14
votes
3 answers

When should [assembly: InternalsVisibleTo()] be used?

I understand that the InternalVisibleTo attribute is used to expose types and methods with the internal access modifier to a specified assembly. I have only ever used this for exposing internal methods to a separate assembly containing a suite of…
fletcher
  • 13,380
  • 9
  • 52
  • 69
14
votes
1 answer

Scala: Accessing package visible methods through structural types outside the package

This does not work as expected (since I am trying to call a package private run from outside Services): object Services { class HelloPrinter { private[Services] def run = "Hello" } } val obj = new Services.HelloPrinter But, surprisingly…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
14
votes
4 answers

internal abstract methods. Why would anyone have them?

I was doing some code review today and came across an old code written by some developer. It goes something like this public abstract class BaseControl { internal abstract void DoSomething(); } If you have a derived class within the same…
ram
  • 11,468
  • 16
  • 63
  • 89
14
votes
7 answers

What is the difference between "private" and "protected Internal"?

I just want to know what is the actual difference between private and protected internal access specifier. As i know Visible to own class members: private and protected internal YES Visible to object of other classes: Both NO Visible to objects of…
avirk
  • 3,050
  • 7
  • 38
  • 57
14
votes
10 answers

Can I override a private method in Java?

I know I can use reflection to invoke a private method, and to get or set the value of a private variable, but I want to override a method. public class SuperClass { public void printInt() { System.out.println("I am " + getClass() + ".…
numbers longer
  • 331
  • 1
  • 3
  • 9
13
votes
1 answer

Public alias for non-public type

I wonder if it is valid C++ : class Test { struct PrivateInner { PrivateInner(std::string const &str) { std::cout << str << "\n"; } }; public: using PublicInner = PrivateInner; }; //Test::PrivateInner…
Johnmph
  • 3,391
  • 24
  • 32
13
votes
6 answers

"public static" vs "static public" - is there a difference?

sealed class PI { public static float number; static PI() { number = 3.141592653F; } static public float val() { return number; } } What's the difference between public static and static public? Can they be used in any order? How would…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
13
votes
2 answers

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; } public virtual string RoadName { get; set; } …
FLSH
  • 343
  • 1
  • 5
  • 15
13
votes
8 answers

Default access modifier in C#

If I will create a new object like the following, which access modifier will it have by default? Object objectA = new Object();
r.r
  • 7,023
  • 28
  • 87
  • 129
13
votes
4 answers

Outside classes accessing package-private methods

Suppose I have a class in my package org.jake and it has a method with default access (no modifier). Then the method is visible inside the package only. However, when someone receives the jar of my framework, what is to stop them from writing a new…
Jake
  • 15,007
  • 22
  • 70
  • 86