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
42
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
  • 10,387
  • 23
  • 54
  • 73
37
votes
7 answers

Why make private inner class member public in Java?

What is the reason of declaring a member of a private inner class public in Java if it still can't be accessed outside of containing class? Or can it? public class DataStructure { // ... private class InnerEvenIterator { // ... …
vitaut
  • 49,672
  • 25
  • 199
  • 336
36
votes
3 answers

Java tutorial says I can have a package-private interface, but I can't

In the Java tutorial "Defining an Interface", it says If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface. However, this interface PPInterface { …
Pete
  • 16,534
  • 9
  • 40
  • 54
35
votes
6 answers

C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field?

I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access. //The base class: class BaseClass { public: BaseClass(std::string); …
dwinchell
  • 405
  • 1
  • 4
  • 6
33
votes
5 answers

Class inheritance: Function is inaccessible

I'm getting this error, but I thought I would only get it if the member's protection level was too high and made it inaccessible, but I'm getting it anyway. Shopable.h: #ifndef _SHOPABLE_H_ #define _SHOPABLE_H_ #include "Library.h" class…
pighead10
  • 4,155
  • 10
  • 34
  • 52
31
votes
6 answers

Difference between the default access specifier and protected access specifier in java

I was trying to learn java and when I went through access specifiers I had a doubt. What is the difference between the default one if none is specified and the protected access specifier in java?
Abhilash PS
  • 734
  • 1
  • 11
  • 25
31
votes
3 answers

What's the access modifier of the default constructor in java?

We all know that if we don't specifically define a constructor, the compiler inserts an invisible zero-parameter constructor. I thought its access modifier was public, but in dealing with an inner class issue, I found maybe I was wrong. Here is my…
jason
  • 364
  • 1
  • 3
  • 10
30
votes
12 answers

Interfaces in Java: cannot make implemented methods protected or private

I know that an interface must be public. However, I don't want that. I want my implemented methods to only be accessible from their own package, so I want my implemented methods to be protected. The problem is I can't make the interface or the…
jbu
  • 15,831
  • 29
  • 82
  • 105
30
votes
1 answer

Constructor accessibility C# compiler error CS0122 vs CS1729

① In following C# code, CS1729 occurs but I understand that CS0122 would be more appropriate. namespace A { class Program { static void Main() { Test test = new Test(1); } } class Test { Test(int i) { } } } CS1729:…
Fabien Launay
  • 639
  • 7
  • 16
29
votes
1 answer

Are there good reasons for 'private' to work the way it does in Ruby?

It took me a while to understand how private methods work in Ruby, and it really strikes me as being very awkward. Does anyone know if there are good reasons for private methods to be handled the way they are? Is it just historic reasons? Or…
MiniQuark
  • 46,633
  • 36
  • 147
  • 183
27
votes
7 answers

Why would a virtual function be private?

I just spotted this in some code: class Foo { [...] private: virtual void Bar() = 0; [...] } Does this have any purpose? (I am trying to port some code from VS to G++, and this caught my attention)
liori
  • 40,917
  • 13
  • 78
  • 105
23
votes
3 answers

Error: Attempt to call private method

Coming from a long history of C-style syntax and now trying to learn Ruby (on Rails), I've been having my share of issues with its idioms and such, but today I hit one I didn't expect to have a problem with and I can't see whatever it is that must…
Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
22
votes
3 answers

Inconsistent accessibility: base class is less accessible than class

So I have an abstract base class in a DLL and child classes of that class. I want the childs to be public, but the base to be private so that it cannot be accessed outside of the dll. How do I do that?
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
21
votes
2 answers

Can I access a base classes protected members from a static function in a derived class?

I have a program where I need to make a base class which is shared between a dll and some application code. Then I have two different derived classes, one in the dll one in the main application. Each of these have some static member functions which…
David Snape
  • 225
  • 2
  • 9
20
votes
3 answers

Calling a private instance method from a class method in Ruby

Can I create a private instance method that can be called by a class method? class Foo def initialize(n) @n = n end private # or protected? def plus(n) @n += n end end class Foo def Foo.bar(my_instance, n) …
user4812
  • 5,942
  • 9
  • 30
  • 35
1
2
3
16 17