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
1
vote
1 answer

Singleton patterns, private methods and singleton module

I am fighting with singleton patterns in Ruby. I know that singleton implements a single instance of an object but I don't quite understand if we can replicate it without the singleton module. Then there is the issue with private methods; Right…
Jordon Bedwell
  • 3,189
  • 3
  • 23
  • 32
1
vote
3 answers

Class member privacy and headers in C++

So I'm making a class to define a character in D&D. The way I thought setting up the class was that public members are defined in the header and private in the .cpp so they're not revealed to the outside right? How do you do this? Currently it looks…
Portaljacker
  • 3,102
  • 5
  • 25
  • 33
1
vote
0 answers

Is there any point in having a public member with a private type?

Consider the following C++ code, where member foo has a private type: class Object { private: struct MyPrivateThing { long double Member; }; using Type = MyPrivateThing; public: Type foo = {32.45L}; // MyPrivateThingFoo…
saxbophone
  • 779
  • 1
  • 6
  • 22
1
vote
3 answers

how to know the access specifier used by the compiler in c

Is there a way to know the access specifier used by the compiler in c. For Example- In the case of register variables, it all depends on the compiler to decide whether a variable's access specifier would be auto or register. Is there a way to…
ankith13
  • 371
  • 1
  • 3
  • 10
1
vote
1 answer

How can be encapsulation broken in package-private declarations in Java

"The encapsulation can be easily broken, because external code can define classes in the same packages used by your code and thus get access to your package-private declarations." I am not able to understand how can we achieve what is written in the…
Sarthak Mittal
  • 135
  • 1
  • 2
  • 11
1
vote
0 answers

VueJs2+typescript - Unable to restrict private variable to get accessed in template

I have worked upon almost all the Angular2+ versions using typescript. I am bit new for VueJs2. Since in Angular we keep public variables which can be used inside HTML template and private variables are mainly used for processing the information in…
Himanshu Saxena
  • 340
  • 3
  • 13
1
vote
1 answer

How can I use a private inner template class as a parameter in a template specialization?

While trying to get some old software to compile with clang, I encountered some code similar to the following: class OuterClass { private: template class InnerClass {}; }; template class OtherClass {}; template
fakedad
  • 1,292
  • 1
  • 10
  • 21
1
vote
0 answers

Can I detect if a class has any public non-static data members?

I'm trying to write code that does this. Something like the following struct A { int i; }; struct B { private : int i; }; static_assert(has_public_non_static_data_members{}); static_assert(! has_public_non_static_data_members{}); But I can't…
cigien
  • 57,834
  • 11
  • 73
  • 112
1
vote
1 answer

Access specifier, not able to call a function which is defined in the derived class

I tried to learn about the access specifier in c++, So i access a public and protected member from the base class and performed a add function in the derived class as shown below, #include using namespace std; class Base { public: …
Jayakumar
  • 153
  • 1
  • 13
1
vote
2 answers

Why the overridden method should be public in this program?

import java.util.*; interface AdvancedArithmetic{ int divisor_sum(int n); } class MyCalculator implements AdvancedArithmetic { int sum=0; int divisor_sum(int n) //Why this method should be public? { for(int i=1;i<=n;i++) …
1
vote
5 answers

C++ access specifiers

I just want to make sure I got the idea of public and private right. Regarding the private access specifier, does it mean: Only accessed inside the class Cannot be accessed from the object of the class unless there are public class methods that can…
Simplicity
  • 47,404
  • 98
  • 256
  • 385
1
vote
1 answer

What is 'Access specifier' in Object Oriented Programming

What is 'Access specifier' in Object oriented programming ? I have searched for it's definition several times but not get the satisfactory answer. Can anyone please explain it to me with realistic example ?.... Thanks in advance
Arafat
  • 143
  • 1
  • 4
  • 14
1
vote
1 answer

How to return the result of a method to outside the method

Sorry if this question is really basic but I'm very new to Java. This is some code that is outside all methods and in a class called "Triangle". These are used by multiple methods in the class: static final int COORDS_PER_VERTEX = 3; static float…
Aswin Abraham
  • 139
  • 1
  • 7
1
vote
2 answers

Rationale for difference in "default" access-specifier for a base class

I know that there're few differences between struct and class in C++. I also understand the reason(s) for few of the difference(s). Like this one, Members of struct are public by default; members of class are private by default. The reason why…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
1
vote
3 answers

Access rights of undefined access member variable (neither public nor private)

What happens to a member variable in C++ if you don't define the access rights? For instance, in the following code: class Base { int myQuestion; public: int myPublic; private: int myPrivate; } Who has access to myQuestion?
busssard
  • 11
  • 1