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.

228 questions
1087
votes
16 answers

Difference between private, public, and protected inheritance

What is the difference between public, private, and protected inheritance in C++? All of the questions I've found on SO deal with specific cases.
user106599
235
votes
8 answers

How to create a private class method?

How come this approach of creating a private class method works: class Person def self.get_name persons_name end class << self private def persons_name "Sam" end end end puts "Hey, " + Person.get_name puts "Hey, "…
99miles
  • 10,382
  • 17
  • 71
  • 118
148
votes
10 answers

What is a good example to differentiate between fileprivate and private in Swift3

This article has been helpful in understanding the new access specifiers in Swift 3. It also gives some examples of different usages of fileprivate and private. My question is - isn't using fileprivate on a function that is going to be used only…
Nikita P
  • 4,066
  • 5
  • 27
  • 53
143
votes
8 answers

Why does Ruby have both private and protected methods?

Before I read this article, I thought access control in Ruby worked like this: public - can be accessed by any object (e.g. Obj.new.public_method) protected - can only be accessed from within the object itself, as well as any subclasses private -…
Kyle Slattery
  • 29,012
  • 9
  • 29
  • 35
136
votes
5 answers

Private virtual method in C++

What is the advantage of making a private method virtual in C++? I have noticed this in an open source C++ project: class HTMLDocument : public Document, public CachedResourceClient { private: virtual bool childAllowed(Node*); virtual…
silverburgh
  • 7,129
  • 10
  • 28
  • 23
116
votes
10 answers

Private module methods in Ruby

I have a two part question Best-Practice I have an algorithm that performs some operation on a data structure using the public interface It is currently a module with numerous static methods, all private except for the one public interface…
Daniel Beardsley
  • 18,289
  • 20
  • 62
  • 76
112
votes
12 answers

What is the default access specifier in Java?

I just started reading a Java book and wondered; which access specifier is the default one, if none is specified?
bennedich
  • 11,188
  • 6
  • 30
  • 40
93
votes
5 answers

Why can I access private variables in the copy constructor?

I have learned that I can never access a private variable, only with a get-function in the class. But then why can I access it in the copy constructor? Example: Field::Field(const Field& f) { pFirst = new T[f.capacity()]; pLast = pFirst +…
demonking
  • 2,275
  • 4
  • 21
  • 27
59
votes
3 answers

How to make the class constructor private in Ruby?

class A private def initialize puts "wtf?" end end A.new #still works and calls initialize and class A private def self.new super.new end end doesn't work altogether So what's the correct way? I want to make new private and call…
Leonid Shevtsov
  • 13,290
  • 8
  • 48
  • 80
52
votes
4 answers

Access to protected member through member-pointer: is it a hack?

We all know members specified protected from a base class can only be accessed from a derived class own instance. This is a feature from the Standard, and this has been discussed on Stack Overflow multiple times: Cannot access protected member of…
YSC
  • 34,418
  • 7
  • 80
  • 129
52
votes
7 answers

Understanding private methods in Ruby

class Example private def example_test puts 'Hello' end end e = Example.new e.example_test This of course will not work, because we specified explicit receiver - instance of Example (e), and that is against a "private rule". But I cannot…
Ernest
  • 7,961
  • 3
  • 33
  • 50
51
votes
3 answers

How to set private instance variable used within a method test?

Given a class with a couple of instance variables and some methods. Some instance variables are set accessible via attr_reader and attr_accessor. Thus the others are private. Some of the private instance variables get set within one of the instance…
Torbjörn
  • 4,822
  • 4
  • 44
  • 70
48
votes
4 answers

How to I make private class constants in Ruby

In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside) class Person SECRET='xxx' # How to make class private?? def show_secret puts "Secret: #{SECRET}" …
DMisener
  • 821
  • 1
  • 7
  • 15
43
votes
9 answers

What is the difference between access specifiers and access modifiers?

In Java, are access specifiers and access modifiers the same thing?
sgokhales
  • 49,762
  • 33
  • 123
  • 158
40
votes
7 answers

What are the differences between "private", "public", and "protected methods"?

I'm learning Ruby, and have come up to a point where I am confused. The book I am using is talking about private, public, and protected methods, but I am still a bit confused. What are the differences between each?
Billjk
  • 9,249
  • 22
  • 49
  • 70
1
2 3
15 16