Questions tagged [access-specifier]

The access specifier in an object-oriented language determines how a class restricts access to its members.

The access specifier in an object-oriented language determines how a class restricts access to its members.

The usual access specifiers are private, protected and public.

  • A class member whose access specifier is private can only be used by instances of that class.
  • A class member whose access specifier is protected can only be used by some other classes.
    Usually this means that access to these members is granted only to subclasses, but this is not a hard-and-fast rule. In Java, for example, classes that are in the same package also have access to each other's protected members.
  • A class member whose access specifier is public can be used by all other classes in the program.

You can use this tag for questions about how access to the members of your class is resolved.

242 questions
0
votes
1 answer

Why can't I access private members of the outer class from the inner class when nesting classes?

class X { int Xi; class Y { int Yi; void func() { X x; x.Xi = 5; } }; void func() { Y y; y.Yi = 5; // ^^^^ 'X::Y::Yi': cannot access private…
StackExchange123
  • 1,871
  • 9
  • 24
0
votes
1 answer

How to avoid access errors while using friend functions in c++?

#include using namespace std; class boss{ int salary; public: boss(); boss(int b){ salary = b; } friend void total(boss, employe); }; class employe{ int salary; friend void total(boss, employe); public: employe(); …
0
votes
1 answer

method defined in class inaccessible

I was following this example: https://www.boraji.com/spring-boot-configurationproperties-example To make a nested list of Java Spring properties, that, obfuscated and simplified, looks like…
Carmageddon
  • 2,627
  • 4
  • 36
  • 56
0
votes
2 answers

how do i initialize a public variable in a public class method

I have a public class in which I create an array, this array takes its size from the constructor and needs to be used in other functions (including int main). Therefore the variable must be public. my code looks something along these lines: class…
user11308226
0
votes
1 answer

Preserving restrictive access modifiers and making a class serializable

I'm trying to make a class I wrote serializable. It contains several static readonly fields that never change after construction, so they don't need to be serialized. I'm having trouble finding a way to re-set them after de-serialization that…
0
votes
1 answer

Get rid or duplicate private static

I have the following code: private static double calcTotalMass() { return bike_mass + human_mass; } private static double calcWeight() { return calcTotalMass() * grav_acc; } private static double calcWork() { return calcWeight() * height;…
Post Self
  • 1,471
  • 2
  • 14
  • 34
0
votes
0 answers

Access Specifier check for virtual functions

class Base{ public: virtual void func() { cout<< "base"; } }; class Derived : public Base{ private: void func(){ cout<< "derived"; } }; int main() { Base *b = new Derived(); b->func(); //output …
Azazel
  • 11
  • 2
0
votes
1 answer

How to make my Objective C class method only visible for Swift classes

I have a common constant file which will have all my objective C macros, to access my objective c macros in swift class I have planned to implement the way suggested in the following article. Way To Access Objective C macros In Swift Basically,…
Peer Mohamed Thabib
  • 636
  • 2
  • 9
  • 29
0
votes
1 answer

c++ access-specifier understanding

I have encountered the following response in a thread : Protected members can be accessed from derived classes. Private ones can't. class Base { private: int MyPrivateInt; protected: int MyProtectedInt; public: int MyPublicInt; }; class…
TomBarons
  • 3
  • 4
0
votes
3 answers

My C# Private Class is accessible anywhere inside the DLL, then whats the use of internal?

I have ClassLibrary project in C# and all my 'private classes' (under different namespace) are accessible to each other inside the same assembly (project). Class1.cs ---------------------------- namespace MyClass1App { private class…
Pavan G R
  • 306
  • 3
  • 16
0
votes
1 answer

Private section swift 3 in Struct / Class definition

In C++ you can specify "public: / private:" sections within your class definitions. Is there a way to do this in Swift 3 or do I have to use the keyword "private" on front of every object I wish to be private?
Makaronodentro
  • 907
  • 1
  • 7
  • 21
0
votes
1 answer

Trying to understand the inheritance chain

Below is Parent & Child class. public class ParentController : ApiController { public ICustomer customer { get; set;} public ICustUtil util { get; set;} } public class ChildController : ParentController { //no issue here public…
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
0
votes
1 answer

How to call method outside class

Is it possible to call method outside class, using an existing instance? I want to read data from sensor and try to send it through BT connection (already connected and sending other informations) but I cannot call write() method from…
JoeDoe
  • 13
  • 1
  • 3
0
votes
2 answers

Why use access specifiers when defining inner (non-static nested) classes in Java?

Consider the following snippet: class Deep { static class StaticInner { } class InnerClass { } public class InnerClass2 { } private class InnerClass3 { } protected class InnerClass4 { } } public…
Fresher
  • 333
  • 1
  • 3
  • 13
0
votes
1 answer

How can I initialize static const class member object referring to private fields?

Here's the problem: a class called Factory has several non-static member functions of the same signature, let's call them f1, f2 and so on. I'd like to put pointers to these member functions in a std::vector that's a static const, as there's no need…
The Vee
  • 11,420
  • 5
  • 27
  • 60