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
42
votes
3 answers

difference between protected and package-private access modifiers in Java?

I have seen various articles on differences between the protected and package private modifiers. One thing I found contradictory between these two posts Isn't "package private" member access synonymous with the default (no-modifier) access? In…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
41
votes
3 answers

Why can't I use protected constructors outside the package?

Why can't I use protected constructors outside the package for this piece of code: package code; public class Example{ protected Example(){} ... } Check.java package test; public class Check extends Example { void m1() { Example…
Abhilash28
  • 645
  • 1
  • 9
  • 15
40
votes
8 answers

accessing a protected member of a base class in another subclass

Why does this compile: class FooBase { protected: void fooBase(void); }; class Foo : public FooBase { public: void foo(Foo& fooBar) { fooBar.fooBase(); } }; but this does not? class FooBase { protected: void…
Kaiserludi
  • 2,434
  • 2
  • 23
  • 41
39
votes
4 answers

Workaround to accomplish protected properties in Objective-C

I've been trying to find a workaround to declare @protected properties in Objective-C so only subclasses in the hierarchy can access them (read only, not write). I read that there is no documented way of doing this so I thought of this workaround…
Alex Salom
  • 3,074
  • 4
  • 22
  • 33
37
votes
3 answers

protected vs public constructor for abstract class? Is there a difference?

This question is out of curiosity. Is there a difference between: public abstract class MyClass { public MyClass() { } } and public abstract class MyClass { protected MyClass() { } } Thanks.
Marlon
  • 19,924
  • 12
  • 70
  • 101
35
votes
6 answers

C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field?

I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access. //The base class: class BaseClass { public: BaseClass(std::string); …
dwinchell
  • 405
  • 1
  • 4
  • 6
33
votes
6 answers

What does the protected modifier mean?

I am reading the book The Java Programming Language, 3rd edition. In chapter 3.5 , it illustrates the protected modifier with the following words: More precisely, beyond being accessible within the class itself and to code within the same…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
33
votes
4 answers

Why can't my subclass access a protected variable of its superclass, when it's in a different package?

I have an abstract class, relation in package database.relation and a subclass of it, Join, in package database.operations. relation has a protected member named mStructure. In Join: public Join(final Relation relLeft, final Relation relRight) { …
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
31
votes
4 answers

Why is protected constructor raising an error this this code?

One question about protected constructor. I learnt that the protected constructor can be used in the derived class. How ever, I found the code below has an error. Why does it happen like this? class A { protected: A(){} }; class B:…
skydoor
  • 25,218
  • 52
  • 147
  • 201
26
votes
1 answer

Can I create a method with Java protected access in Scala?

I have a Java class that I have ported to Scala, but which still has some Java subclasses implementing abstract functionality. The original Java had public abstract class Base { ... protected abstract Foo getFoo(); } which was…
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
26
votes
2 answers

Revert a merge commit from a protected branch on GitHub.com

We have protected our develop branch on GitHub so that nobody downstream can push their commit directly. The commits need to go through specific feature branch and get merged through a pull request. There came a scenario where a feature branch is…
nak
  • 846
  • 2
  • 10
  • 26
26
votes
8 answers

Can I/How to... call a protected function outside of a class in PHP

I have a protected function that is defined within a certain class. I want to be able to call this protected function outside of the class within another function. Is this possible and if so how may I achieve it class cExample{ protected…
Terry Hoverter
  • 263
  • 1
  • 3
  • 4
23
votes
5 answers

Accessing protected method in test case using Java Reflection

Am trying to obtain and invoke a protected method residing in a different class and also different package using Java Reflection. Class containing protected method: package com.myapp; public class MyServiceImpl { protected List
PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
23
votes
7 answers

Protected Keyword C#

I want to know what is the meaning of protected in C#, why we use it, and the benefit of the keyword? For instance protected int currentColorIndex; Please elaborate.
Nishant Kumar
  • 5,995
  • 19
  • 69
  • 95
23
votes
6 answers

How do I unit test a protected method in C++?

How do I unit test a protected method in C++? In Java, I'd either create the test class in the same package as the class under test or create an anonymous subclass that exposes the method I need in my test class, but neither of those methods are…
Alex B
  • 24,678
  • 14
  • 64
  • 87
1 2
3
80 81