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
3
votes
1 answer

Fine grained access specifiers c++

I have the following class :- class A { public: // some stuff that everyone should see protected: // some stuff that derived classes should see private: // some stuff that only I can see void f(); void g(); }; Now, I want…
owagh
  • 3,428
  • 2
  • 31
  • 53
2
votes
5 answers

Don't the Ruby methods instance_eval() and send() negate the benefits of private visibility?

w = Widget.new # Create a Widget w.send :utility_method # Invoke private method! w.instance_eval { utility_method } # Another way to invoke it w.instance_eval { @x } # Read instance variable of w Looking at the example above which relates to the…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
2
votes
1 answer

Top level methods: Why they become private instance methods in Object? Why not public?

What is the reasoning behind making top level methods in Ruby private instance methods of Object? If they were somehow made public how would that change things?
slindsey3000
  • 4,053
  • 5
  • 36
  • 56
2
votes
1 answer

Using a private inner class as template parameter default value (gcc vs clang)

I use the (simplified) following code to implement pimpl idiom // in .h #include template class Pimpl { public: Pimpl() : m_impl(std::make_unique()) {} Impl * operator->() { return…
2
votes
4 answers

Is it considered good practice to change the protection level of a method?

In other words if I have a class class A { public: A() { .. } virtual void somemethod() { .. } }; is it ok to write class B : public A { public: B() { .. } protected: virtual void somemethod() { .. } }; or are there some drawbacks with…
AndersK
  • 35,813
  • 6
  • 60
  • 86
2
votes
1 answer

Why a friend class member can access a private member of the class it is being friend to through an object of publicly inherited class?

As we know a private member is inaccessible (not just private) in derived classes while public and protected are directly accessible there. If a class declares another class as a friend then the latter has full-access to the members of the…
Maestro
  • 2,512
  • 9
  • 24
2
votes
1 answer

Private class member accessible outside class

Why are we allowed to assign public access specifier for a member in a private class i.e. incorrectVariable in the code below: My code doesn't give compilation error and run properly, my code is: private class C { public var incorrectVariable =…
Vivek Tyagi
  • 168
  • 1
  • 4
  • 15
2
votes
3 answers

Ruby Class#new - Why is `new` a private method?

I made a Matrix class and I want to use it in various parts of my code. class Matrix def initialize(x, y, v=0) @matrix = Array.new (0..y).each do |j| @matrix[j] = Array.new (0..x).each do |i| @matrix[j][i] = v …
Austin Richardson
  • 8,078
  • 13
  • 43
  • 49
2
votes
1 answer

Protected and Private methods

I'm reading through Beginning Ruby and I'm stuck at the part about private and protected methods. This is a newbie question, I know. I searched through SO for a bit but I couldn't manage to find a clear and newbie-friendly explanation of the…
Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
2
votes
5 answers

Access-specifiers are not foolproof?

If I've a class like this, class Sample { private: int X; }; Then we cannot access X from outside, so this is illegal, Sample s; s.X = 10; // error - private access But we can make it accessible without editing the class! All we need…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
2
votes
2 answers

Why does calling of test() function of Foo class (which extends Bar class) returns result mixed from both classes?

It may sound silly, but I'm new in PHP. I was learning from the documentation about access specifiers when I came to this section. class Bar { public function __construct() { echo "Bar::constructor
"; } public function…
Bivek
  • 1,380
  • 10
  • 24
2
votes
3 answers

Clarify definitions of "private" and "protected" in Ruby?

If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private…
Jegan
  • 185
  • 1
  • 1
  • 13
2
votes
2 answers

Why can't we specify default access modifier for a class

I know why private and protected are illegal modifiers for a class but why is default not acceptable. I think if we don't specify any access modifier, default gets assigned. But typing it explicitly like default class Student gives me a syntax error
Manoj
  • 644
  • 12
  • 21
2
votes
2 answers

Preferred Ruby-ist way of declaring access controls

This is a simple style question. What is the preferred means for declaring access controls in Ruby code? Example A: #!/usr/bin/env ruby class MyClass def method1 # this is public by default #... end protected #…
2
votes
3 answers

Is there a way to make an array truly private in Java?

If I do something like private int[] myPrivateArray; and have a getter that returns the array, then the array contents can be modified from outside the class, which sort of defeats the purpose of making it private.
Dylanthepiguy
  • 1,621
  • 18
  • 47