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
16
votes
2 answers

Java: accessing protected fields from inner class

Recently I've faced a problem getting a runtime error java.lang.IllegalAccessError when trying to access from inner class a protected field declared in outer's parent class that was loaded by a different class loader. Briefly: Class Parent has…
Timofei Davydik
  • 7,244
  • 7
  • 33
  • 59
16
votes
6 answers

Can you ever have too many "protected virtual" methods?

Here's a question for those of you with experience in larger projects and API/framework design. I am working on a framework that will be used by many other projects in the future, so I want to make it nice and extensible, but at the same time it…
cbp
  • 25,252
  • 29
  • 125
  • 205
15
votes
4 answers

iOS distribution - parameters in itms-services protocol link for plist

I would like to pass the userid and password in the itms-services link so that the protected plist can be accessed. To clarify, in the following link, the plist cannot be accessed directly as the access requires the userid and password to be…
Pradeep
  • 151
  • 1
  • 1
  • 3
15
votes
14 answers

Should protected attributes always be banned?

I seldom use inheritance, but when I do, I never use protected attributes because I think it breaks the encapsulation of the inherited classes. Do you use protected attributes ? what do you use them for ?
Stephane
  • 868
  • 6
  • 10
15
votes
7 answers

Access to method pointer to protected method?

This code: class B { protected: void Foo(){} } class D : public B { public: void Baz() { Foo(); } void Bar() { printf("%x\n", &B::Foo); } } gives this error: t.cpp: In member function 'void D::Bar()': Line 3: error: 'void…
BCS
  • 75,627
  • 68
  • 187
  • 294
15
votes
9 answers

Protected member access from different packages in java - a curiosity

package packageOne; public class Base { protected void display(){ System.out.println("in Base"); } } package packageTwo; public class Derived extends packageOne.Base { public void show(){ new Base().display(); //this is…
abson
  • 9,148
  • 17
  • 50
  • 69
15
votes
3 answers

Protected fields not visible to subclasses

I'm writing a custom view that directly extends android.view.View. If I try to access fields mScrollX or mScrollY, I see an error that the field "cannot be resolved or is not a field." The source code for android.view.View has mScrollX, mScrollY,…
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
15
votes
2 answers

Protected static member variables

I've recently been working on some class files and I've noticed that the member variables had been set in a protected static mode like protected static $_someVar and accessed like static::$_someVar. I understand the concept of visibility and that…
user275074
15
votes
2 answers

Difference between protected and protected[this]

I have the following code: class Base{ protected val alpha ="Alpha"; protected def sayHello = "Hello"; } class Derived extends Base{ val base = new Base; def hello = println(this.alpha +" "+this.sayHello) ; // def…
optional
  • 3,260
  • 5
  • 26
  • 47
14
votes
2 answers

Do you ever use protected visibility in Rails?

Confession: I only use private and public visibility for my methods! I have a feeling this is a bad thing. But in Rails it just doesn't seem to come up as an issue. Does anyone have an example in Rails where it would be a big mistake not to use…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
14
votes
6 answers

Why can't you have a protected abstract class in Java?

I have an abstract class which looks like: abstract class AbstractFoo implements Bar { //Code goes here } However when I try to make AbstractFoo protected I get an error compile time error complaining that it is an illegal modifier. protected…
binarycreations
  • 3,091
  • 6
  • 32
  • 38
14
votes
8 answers

How do you access protected Java method in thirdparty library?

Assume that you must access a protected method of a Java object that you receive somewhere in your code. What is your solution? I know one approach: You can employ reflection and call setAccessible(true) on the Method object. Any other idea?
salman.mirghasemi
  • 1,009
  • 2
  • 7
  • 15
14
votes
3 answers

Why can't I call BIOS interrupts from protected mode?

Right. I've spent over three hours today trying to understand why you can't call a bios ISR when in protected mode. I get that once you set and IDT it wont necessarily be in the usual address for the IVT plus segments dont have a fixed size in…
Max
  • 155
  • 1
  • 4
14
votes
4 answers

Java: protected access across packages

I would like to understand what's happening in the example below (where a protected member is being accessed from outside the package through a subclass). I know for classes outside the package, the subclass can see the protected member only through…
JWhiz
  • 681
  • 3
  • 10
  • 22
14
votes
3 answers

Why protected superclass member cannot be accessed in a subclass function when passed as an argument?

I get a compile error, which I'm slightly confused about. This is on VS2003. error C2248: 'A::y' : cannot access protected member declared in class 'A' class A { public: A() : x(0), y(0) {} protected: int x; int y; }; class B : public…
Chris Bednarski
  • 3,364
  • 25
  • 33