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
2 answers

Is this a correct summary on member access rules?

I am trying to fully understand member access rules defined in multiple paragraphs of the [class.access] section of the C++ standard. They are quite complex or even confusing therefore I need a short but accurate and exhaustive summary. I compiled…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
3
votes
1 answer

Access protected members of base class in grandchild class

I have a parent class containing attribute declared as protected. I know protected members can be accessed within child class. But how the same can be accessed in grandchild class. For e.g, How to access width in TooSmall class? Consider this code…
Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
3
votes
3 answers

Accessing Inherited Functions

In multiple inheritance,where all the base class contains same function name with different functionality, we can access the protected function from particular base class using "::" scope resolution operator. However, I tried something else. I…
Prasad Barude
  • 31
  • 1
  • 3
3
votes
1 answer

Getting around access specifiers with C++0x decltype

Consider the following code: class A { private: class B {}; public: B f(); }; A a; A::B g() { return a.f(); } The compiler rejects this - g cannot return A::B because A::B is private. But suppose I now use decltype to specify the…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
3
votes
1 answer

Difference between Exports, Indirect Exports, Requires and Indirect Requires in Java9?

I was going through the API docs for jdk.accessibility and noticed that it lists all these separately. The modules with scope requires in one module are all consuming module's Indirect Requires? The module-info.java for the module is as…
Naman
  • 27,789
  • 26
  • 218
  • 353
3
votes
5 answers

Why should I declare implemented interface methods as "public"?

interface Rideable { String getGait(); } public class Camel implements Rideable { int weight = 2; String getGait() { return " mph, lope"; } void go(int speed) {++speed; weight++; int walkrate = speed *…
Akhil Vijayan
  • 119
  • 2
  • 11
3
votes
2 answers

What's the advantage of making methods public in an interface, but protected in the implementation?

In my C++ application I have an interface that looks like this: class ICalculator { public: virtual double calculateValue(double d) = 0; }; I have implementations of this interface that look like this: class MySpecificCalculator …
Patrick
  • 23,217
  • 12
  • 67
  • 130
3
votes
2 answers

How to do package-private classes visible for other one package only?

I want to implement package with optimal encapsulation, but test it. How can I do package-private members of one package visible for other one (friendly) package only?
3
votes
2 answers

Access private variable of class in method of class c++

I'm trying to understand why the object of the same class can access private member of each other. Actually I know that The access modifiers work on class level, and not on object level. From here. But I don't understand reasons of it. I only can…
segevara
  • 610
  • 1
  • 7
  • 18
3
votes
4 answers

Accessing Sub functions /procedures from DPR or other function / procedure in Delphi

As much I know - Subroutines are with Private access mode to its parent unction / procedure, right? Is there any way to access them from "outer-world" - dpr or other function / procedure in unit? Also - which way takes more calcualtion and space to…
HX_unbanned
  • 583
  • 1
  • 15
  • 51
3
votes
1 answer

private public protected access specifiers in python

Can we simulate private and protected access specifiers in python? Name Mangling eg: __var=10 can simulate private but its viable to be accessed outside easily via the object. object._className__var So is there a way we could simulate or does…
deeshank
  • 4,286
  • 4
  • 26
  • 32
3
votes
3 answers

Intruding privacy - How does the C++ standard handle it?

Consider the below code snippet. The method Sayhi() is having public access in class Base. Sayhi() has been overridden as a private method by the class Derived. In this way, we can intrude into someone's privacy and C++ has no way to detect it…
Arun
  • 2,087
  • 2
  • 20
  • 33
3
votes
5 answers

Are the private members of superClass inherited by a subClass... Java?

I have gone through this: Do subclasses inherit private fields? But I'm still confused... I'm talking about inheriting only and not accessing. I know that they aren't visible out side class. But does the subclass' object has it's own copies of those…
Sam
  • 1,842
  • 3
  • 19
  • 33
3
votes
2 answers

protected access type

Possible Duplicate: In Java, what's the difference between public, default, protected, and private? Why can't a subclass in one package access the protected member of it's superclass (in another package) by the reference of the superclass? I am…
vijay
  • 321
  • 1
  • 3
  • 5
3
votes
6 answers

Why access specifiers can't be used for variables declared inside method in a Java Class?

Why we can't use access specifiers for variables declared inside method in a Java Class?
Vikram
  • 3,996
  • 8
  • 37
  • 58