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
19
votes
9 answers

C++ Is private really private?

I was trying out the validity of private access specifier in C++. Here goes: Interface: // class_A.h class A { public: void printX(); private: void actualPrintX(); int x; }; Implementation: // class_A.cpp void A::printX() { …
legends2k
  • 31,634
  • 25
  • 118
  • 222
17
votes
6 answers

Why is it allowed to call derived class' private virtual method via pointer of base class?

# include using namespace std; class A { public: virtual void f() { cout << "A::f()" << endl; } }; class B:public A { private: virtual void f() { cout << "B::f()" << endl; } }; int…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
17
votes
8 answers

Is there a way to call a private Class method from an instance in Ruby?

Other than self.class.send :method, args..., of course. I'd like to make a rather complex method available at both the class and instance level without duplicating the code. UPDATE: @Jonathan Branam: that was my assumption, but I wanted to make…
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
16
votes
8 answers

Is it possible to hide or lower access to Inherited Methods in Java?

I have a class structure where I would like some methods in a base class to be accessible from classes derived directly from the base class, but not classes derived from derived classes. According to the Java Language specification it is possible…
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
15
votes
2 answers

respond_to? and protected methods

It may not be so obvious how respond_to? works in ruby. Consider that: class A def public_method end protected def protected_method end private def private_method end end obj = A.new obj.respond_to?(:public_method) #…
mlomnicki
  • 167
  • 1
  • 10
15
votes
3 answers

Why == overloading can access private members of argument

Possible Duplicate: why private value of the obj can be changed by class instance? Consider the following (partial) code: class Group { private: int id; public: void set_id(int); int get_id(); bool…
WeaklyTyped
  • 1,331
  • 4
  • 16
  • 31
14
votes
2 answers

Accessing fileprivate and private variables in extension and another class using swift 4

I have been going through the recent swift docs and working out on few examples in understanding private and fileprivate keywords in swift4. I am trying to access a fileprivate and private variable in an extension of the same class and another class…
arun_K
  • 313
  • 1
  • 4
  • 12
14
votes
1 answer

Public vs Private inner classes in Java

I was reading introduction to Java programming and it does not have a good explanation regarding this topic and it made me wonder why should someone use a private inner class in java instead of using a public one. They both can be used only by the…
Lind
  • 233
  • 1
  • 5
  • 14
13
votes
3 answers

How do I dynamically define a method as private?

This does not seem to work: class Test private define_method :private_method do "uh!" end end puts Test.new.private_method
knoopx
  • 17,089
  • 7
  • 36
  • 41
12
votes
10 answers

How to Access package private Class from a Class in some other package?

I have following classses Hello.java package speak.hello; import java.util.Map; import speak.hi.CustomMap; import speak.hi.Hi; public class Hello { private Hi hi; Hello(Hi hi) { this.hi = hi; } public String sayHello()…
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
12
votes
1 answer

`public` access qualifier and `const`ness. `-Wuninitialized`

class Foo { public: const int x; }; class Bar { private: const int x; }; Output: test.cpp:10:13: warning: non-static const member ‘const int Bar::x’ in class without a constructor [-Wuninitialized] Why does Barproduce a…
aiao
  • 4,621
  • 3
  • 25
  • 47
11
votes
2 answers

Ruby class initialize (constructor) is private method or public method?

Is initialize method (constructor) private or public in ruby?
Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
11
votes
3 answers

On ruby, why include is private and extend is public?

On ruby, what is the reason for include is private, while Object#extend is public?
Vlad Zloteanu
  • 8,464
  • 3
  • 41
  • 58
11
votes
1 answer

Why is it legal to inappropriately access privates in an explicit instantiation?

Why on earth would this be allowed: ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// template struct invisible { static typename…
David
  • 27,652
  • 18
  • 89
  • 138
11
votes
3 answers

c++ like protected in java

Possible Duplicate: How to make data member accessible to class and subclass only In java, protected members can be accessed from within the class, its subclasses and from all the classes present in same package, but i want a member to be…
Eight
  • 4,194
  • 5
  • 30
  • 51
1 2
3
16 17