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.
1100 questions
3352
votes
29 answers

What is the difference between public, protected, package-private and private in Java?

In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance?
intrepion
  • 34,999
  • 4
  • 21
  • 21
1050
votes
17 answers

What is the difference between public, private, and protected?

When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them? Examples: // Public public $variable; public function doSomething() { // ... } // Private private…
Adam Halasz
  • 51,803
  • 63
  • 138
  • 208
301
votes
17 answers

What is the difference between private and protected members of C++ classes?

What is the difference between private and protected members in C++ classes? I understand from best practice conventions that variables and functions which are not called outside the class should be made private—but looking at my MFC project, MFC…
Konrad
  • 35,953
  • 29
  • 74
  • 111
141
votes
16 answers

What's the best way to unit test protected & private methods in Ruby?

What's the best way to unit test protected and private methods in Ruby, using the standard Ruby Test::Unit framework? I'm sure somebody will pipe up and dogmatically assert that "you should only unit test public methods; if it needs unit testing, it…
Brent Chapman
  • 2,459
  • 3
  • 20
  • 10
127
votes
8 answers

Why we should not use protected static in java

I was going through this question Is there a way to override class variables in Java? The first comment with 36 upvotes was: If you ever see a protected static, run. Can anyone explain why is a protected static frowned upon?
Zeeshan
  • 9,465
  • 14
  • 65
  • 93
105
votes
1 answer

Why is Java's AbstractList's removeRange() method protected?

Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList) is protected? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to subclass the List implementation. Is there some…
Joonas Pulakka
  • 34,943
  • 25
  • 103
  • 165
99
votes
10 answers

Should you ever use protected member variables?

Should you ever use protected member variables? What are the the advantages and what issues can this cause?
John Channing
  • 6,303
  • 6
  • 39
  • 56
96
votes
7 answers

Isn't "package private" member access synonymous with the default (no-modifier) access?

I am a little confused over the term "package private" that some of the documentation uses, along with the usage of "default access." Aren't package-private and default access both synonymous with protected?
TurtleToes
  • 1,967
  • 2
  • 16
  • 16
96
votes
9 answers

What are practical uses of a protected constructor?

Why would anyone declare a constructor protected? I know that constructors are declared private for the purpose of not allowing their creation on stack.
Amol Gawai
  • 2,890
  • 2
  • 20
  • 25
95
votes
12 answers

Why can a class not be defined as protected?

Why can we not define a class as protected? I know that we can't, but why? There should be some specific reason.
M.J.
  • 14,866
  • 25
  • 70
  • 95
91
votes
6 answers

Inheritance of private and protected methods in Python

I know, there are no 'real' private/protected methods in Python. This approach isn't meant to hide anything; I just want to understand what Python does. class Parent(object): def _protected(self): pass def __private(self): …
uloco
  • 1,913
  • 3
  • 17
  • 34
82
votes
5 answers

Protected and private methods in Rails

Method visibility in Ruby (public, protected, and private methods) has been well explained in places like this blog post. But in Ruby on Rails it seems slightly different than it would be in a regular Ruby application because of the way the…
jrdioko
  • 28,317
  • 26
  • 75
  • 116
78
votes
14 answers

Why can't I have protected interface members?

What is the argument against declaring protected-access members on interfaces? This, for example, is invalid: public interface IOrange { public OrangePeel Peel { get; } protected OrangePips Seeds { get; } } In this example, the interface…
ajlane
  • 1,789
  • 1
  • 17
  • 22
74
votes
7 answers

Unit testing C# protected methods

I come from the Java EE world but now I'm working on a .Net project. In Java when I wanted to test a protected method it was quite easy, just having the test class with the same package name was enough. Is there anything similar for C#? Is there any…
fiso
  • 1,185
  • 1
  • 12
  • 20
64
votes
7 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
  • 7,995
  • 11
  • 42
  • 45
1
2 3
73 74