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
22
votes
4 answers

Properties for Class and Its Subclasses Only

Is it possible to define properties that are only available to the class they are defined in, and that class's subclasses? Stated another way, is there a way to define protected properties?
Jay Haase
  • 1,979
  • 1
  • 16
  • 36
21
votes
1 answer

Can a static method in a derived class call a protected constructor in C++?

This code works with clang but g++ says: error: ‘A::A()’ is protected class A { protected: A() {} }; class B : public A { static A f() { return A(); } // GCC claims this is an error }; Which compiler is right?
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
21
votes
4 answers

Cannot access protected member of base class in derived class

I have the following code: struct A { protected: A() {} A* a; }; struct B : A { protected: B() { b.a = &b; } A b; }; It strangely doesn't compile. The culprit is the b.a = &b; assignment: both GCC and clang complain that A() is…
user283145
20
votes
8 answers

Why subclass in another package cannot access a protected method?

I have two classes in two different packages: package package1; public class Class1 { public void tryMePublic() { } protected void tryMeProtected() { } } package package2; import package1.Class1; public class Class2 extends…
user2986848
  • 217
  • 1
  • 2
  • 3
20
votes
5 answers

How to open a password protected excel file using python?

I looked at the previous threads regarding this topic, but they have not helped solve the problem. how to read password protected excel in python How to open write reserved excel file in python with win32com? I'm trying to open a password…
Schack
  • 833
  • 2
  • 7
  • 18
20
votes
6 answers

What's the real reason for preventing protected member access through a base/sibling class?

I recently discovered that a method in a derived class can only access the base class's protected instance members through an instance of the derived class (or one of its subclasses): class Base { protected virtual void Member() { } } class…
Tara McGrew
  • 1,987
  • 19
  • 28
19
votes
5 answers

Is there a way to forbid subclassing of my class?

Say I've got a class called "Base", and a class called "Derived" which is a subclass of Base and accesses protected methods and members of Base. What I want to do now is make it so that no other classes can subclass Derived. In Java I can…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
18
votes
8 answers

protected data in abstract class

My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters used only. Now, I understand we want to shield data from direct manipulation…
cvsdave
  • 1,576
  • 2
  • 12
  • 18
18
votes
2 answers

Is it possible to mock protected properties and methods

Is it possible to mock a protected property with PHP Mockery? I got a class with a method, I will call it `a, that does some magic on an array that is retrieved from a protected property from the same class. That protected property is filled by…
Ilyes512
  • 2,627
  • 5
  • 23
  • 27
17
votes
1 answer

Python - Access to a protected member _ of a class

Given a class with some protected members and a public interface to modify them, when is it generally accepted to access the protected members directly? I have some specific examples in mind: Unit testing Internal private methods such as __add__ or…
Raven
  • 648
  • 1
  • 7
  • 18
17
votes
8 answers

Accessing a protected member variable outside a class

I'm querying for the ID of a field by accessing a class function which someone has already put in place. The result is a object returned with protected member variables. I'm struggling to see how I can access the member variable values outside the…
user275074
17
votes
5 answers

In Java, why can a protected member be accessed from outside the class within the same package?

In his book, Herbert Schildt says in page 172 (3rd paragraph) that "protected applies only when inheritance is involved.". In page 228, Table 9-1 shows that a protected member can be accessed from a non-sub class within the same package. The…
PS Nayak
  • 409
  • 8
  • 20
17
votes
2 answers

What is the practical use of protected inheritance?

Public inheritance is easy. A : public B means every A is a B. In most programming language, like vb.net and objective-c, this is the only type of inheritance. Private inheritance is also easy but pointless A : private B means A is implemented by B.…
user4951
  • 32,206
  • 53
  • 172
  • 282
17
votes
1 answer

Why protected can be access in same Package without inheritance in java?

Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N …
motaz99
  • 471
  • 3
  • 8
  • 15
17
votes
9 answers

Testing private class member in C++ without friend

Today I had a discussion with a colleague on whether to test or not to test private members or private state in the class. He almost convinced me why it makes sense. This question does not aim to duplicate already existing StackOverflow questions…
ovanes
  • 5,483
  • 2
  • 34
  • 60