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
14
votes
1 answer

internal interface *less* accessible than an internal protected constructor?

I have an interface and an abstract base class defined in the same assembly: IFoo.cs: internal interface IFoo { ... } Base.cs: public abstract class Base { internal protected Base(IFoo foo) { ... } } This generates the following compiler…
Scott Smith
  • 3,900
  • 2
  • 31
  • 63
14
votes
6 answers

Does static class contain only static method in java?

I have found code in which static class contains methods that are not declared static. Compiler doesn't display any warning. It seems confusing, doesn't it?
user710818
  • 23,228
  • 58
  • 149
  • 207
13
votes
5 answers

Is there a use for making a protected destructor virtual?

/*Child is inherited from Parent*/ class Parent { public: Parent () //Constructor { cout << "\n Parent constructor called\n" << endl; } protected: ~Parent() //Dtor { cout << "\n Parent destructor called\n"…
tusharfloyd
  • 1,732
  • 3
  • 14
  • 18
13
votes
4 answers

Overriding protected methods in Java

Test.java package a; import b.B; public class Test { public static void main(String[] v) { new A().test(); new B().test(); } } A.java: package a; public class A { protected void test() { } } B.java: package b; public…
CromTheDestroyer
  • 3,616
  • 3
  • 20
  • 26
13
votes
2 answers

Best practices to test protected methods with PHPUnit (on abstract classes)

With PHPUnit and PHP >= 5.3 it is possible to test protected methods. The following page at stackoverflow outlined the best practice on it: "Best practices to test protected methods with PHPUnit" protected static function callProtectedMethod($name,…
Daniela Waranie
  • 161
  • 1
  • 1
  • 4
13
votes
2 answers

std::bind()-ing a base protected member function from a derived class's member function

I want to bind() to my base class's version of a function from the derived class. The function is marked protected in the base. When I do so, the code compiles happily in Clang (Apple LLVM Compiler 4.1) but gives an error in both g++ 4.7.2 and in…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
13
votes
4 answers

C++ why use public, private or protected inheritance?

Well there is enough information about this subject. For example this thread was very clear to me: Difference between private, public, and protected inheritance Except one point; Why is it useful?
Tim
  • 5,521
  • 8
  • 36
  • 69
13
votes
2 answers

protected internal

The C# Language Reference on MSDN defines 'protected internal' as "Access is limited to the current assembly or types derived from the containing class". But from a semantic point of view, 'protected internal' sounds to me like 'both protected and…
Asegid Debebe
  • 165
  • 1
  • 8
12
votes
2 answers

C++: Is It OK to Inherit from a Class and Its Protected Member Type?

Is the following code C++ standard compliant? struct B { protected: struct Type {}; }; struct D : B, B::Type {}; int main() { D d; return 0; } I tried it on Compiler Explorer. MSVC(VS 2017 RTW) accepts it. gcc(7.3) and clang(6.0.0)…
zwhconst
  • 1,352
  • 9
  • 19
12
votes
7 answers

Public and private inheritance in C++

As we know from the literature for the public inheritance the object of child class (sub-class) also can be considered as the object of base class (super-class). Why the object of the sub-class can’t be considered as an object of super-class, when…
Narek
  • 38,779
  • 79
  • 233
  • 389
12
votes
3 answers

How are java.lang.Object's protected methods protected from subclasses?

The keyword protected grants access to classes in the same package and subclasses (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html). Now, every class has java.lang.Object as superclass…
Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
12
votes
2 answers

Ruby protected visibility calling from superclass

I have this little code that seems to contradict in some way Ruby's documentation: The second visibility is protected. When calling a protected method the sender must be a subclass of the receiver or the receiver must be a subclass of the sender.…
user1868607
  • 2,558
  • 1
  • 17
  • 38
12
votes
2 answers

What is the purpose of protected modifier for static methods

I found such an usage of protected modifier while searching for a solution for my other question: Ignoring case in strings with unitils ReflectionComparator In the org.unitils.reflectionassert.ReflectionComparatorFactory class there is a method with…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
12
votes
5 answers

C# protected members accessed via base class variable

It may seems rather newbie question, but can you explain why method Der.B() cannot access protected Foo via Base class variable? This looks weird to me: public class Base { protected int Foo; } public class Der : Base { private void B(Base…
Roman
  • 4,531
  • 10
  • 40
  • 69
12
votes
8 answers

Accessing protected member functions from test code in C++

I've been racking my brain trying to think of the best way to access a protected member function from some test code in C++, here's my problem: //in Foo.h Class Foo { protected: void DoSomething(Data data); } //in Blah.h Class Blah { public: …
sarah
  • 145
  • 1
  • 2
  • 6