Questions tagged [protected]

`protected` is an access specifier in object-oriented languages. When the members of a class are `protected`, there is restricted access to these members for other classes.

The protected keyword specifies access to class members in the member-list up to the next access specifier (public or private) or the end of the class definition. Class members declared as protected can be used only by the following:

  • Member functions of the class that originally declared these members.
  • Friends of the class that originally declared these members.
  • Classes derived with public or protected access from the class that originally declared these members.
  • Direct privately derived classes that also have private access to protected members.
  • In the Java language, classes in the same package as the class that originally declared these members.
1204 questions
70
votes
8 answers

Why do we actually need Private or Protected inheritance in C++?

In C++, I can't think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful?
Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
62
votes
2 answers

Changed rules for protected constructors in C++17?

I have this test case: struct A{ protected: A(){} }; struct B: A{}; struct C: A{ C(){} }; struct D: A{ D() = default; }; int main(){ (void)B{}; (void)C{}; (void)D{}; } Both gcc and clang compile it in C++11 and C++14 mode. Both fail in…
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
59
votes
5 answers

JavaDoc for private / protected methods?

Should I write JavaDoc for private or protected methods? And what about private variables? I see class examples on my Java book and the private variables are JavaDoc'ed. So I can't understand if it is a good practice to JavaDoc the private (or…
smartmouse
  • 13,912
  • 34
  • 100
  • 166
55
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
  • 38,212
  • 9
  • 96
  • 149
55
votes
7 answers

How do I use Mockito to mock a protected method?

I’m using Mockito 1.9.5. How do I mock what is coming back from a protected method? I have this protected method … protected JSONObject myMethod(final String param1, final String param2) { … } However, when I attempt to do this in JUnit: …
Dave
  • 15,639
  • 133
  • 442
  • 830
50
votes
11 answers

Java reflection - access protected field

How can I access an inherited protected field from an object by reflection?
Moro
  • 2,088
  • 6
  • 25
  • 34
50
votes
6 answers

subtle C++ inheritance error with protected fields

Below is a subtle example of accessing an instance's protected field x. B is a subclass of A so any variable of type B is also of type A. Why can B::foo() access b's x field, but not a's x field? class A { protected: int x; }; class B : public A…
wcochran
  • 10,089
  • 6
  • 61
  • 69
50
votes
2 answers

What are access specifiers? Should I inherit with private, protected or public?

I am confused about the meaning of access modifiers with respect to inheritance. What is the difference between inheritance involving the private, protected and public keywords?
Sista
  • 887
  • 2
  • 15
  • 19
48
votes
6 answers

Why does the "protected" modifier in Java allow access to other classes in same package?

What is the reason that in Java, a member with a "protected" modifier can not only be accessed by the same class and by subclasses, but also by everyone in the same package? I am wondering about language design reasons, not actual applications…
Uri
  • 88,451
  • 51
  • 221
  • 321
48
votes
1 answer

How to protect classes so they are not visible outside their package

I'd like to be able to have two "protected" classes in my package. That is, I do not want files outside of my package to see them as visible - they will be for internal use within the package only. How can I do this?
Cam
  • 14,930
  • 16
  • 77
  • 128
45
votes
5 answers

What is the difference between protected and private?

I don't understand the difference between protected and private members or methods, as I assumed both will hide the member or the function to access from outside the class. What is the difference between the protected and the private keywords?
danijar
  • 32,406
  • 45
  • 166
  • 297
44
votes
7 answers

Why can't I access C# protected members except like this?

This code: abstract class C { protected abstract void F(D d); } class D : C { protected override void F(D d) { } void G(C c) { c.F(this); } } Generates this error: Cannot access protected member 'C.F(D)' via a…
BCS
  • 75,627
  • 68
  • 187
  • 294
43
votes
4 answers

C++ classes (public, private, and protected)

How can classes in C++ be declared public, private, or protected?
Simplicity
  • 47,404
  • 98
  • 256
  • 385
43
votes
6 answers

Understanding Java's protected modifier

I have a class called A in package1 and another class called C in package2. Class C extends class A. A has an instance variable which is declared like this: protected int protectedInt = 1; Here is the code for class A package package1; public…
mahela007
  • 1,399
  • 4
  • 19
  • 29
42
votes
8 answers

Reasons to use private instead of protected for fields and methods

This is a rather basic OO question, but one that's been bugging me for some time. I tend to avoid using the 'private' visibility modifier for my fields and methods in favor of protected. This is because, generally, I don't see any use in hiding the…
Silvio Donnini
  • 3,233
  • 2
  • 28
  • 29
1
2
3
80 81