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

Why is 'private' used in object oriented program?

There are 'public', 'private', and 'protected' in oop like c++ language. And I tried two kinds of simple programs. Below is first case in c++. class A { public: string name; } int main(void) { A a; a.name; } And, second…
Jitoon
  • 3
  • 2
-2
votes
2 answers

Java Why isn't it allowed to set a protected final field from a subclass constructor?

Why isn't it allowed to set a protected final field from a subclass constructor? Example: class A { protected final boolean b; protected A() { b = false; } } class B extends A { public B() { super(); b =…
stonar96
  • 1,359
  • 2
  • 11
  • 39
-2
votes
1 answer

PHP protected and private properties?

I have a class in PHP that upon an instance being created takes an instance of itself as an argument. A mock of the class is below. abstract class AAA { protected $_a; protected $_b; public function __construct($a, $b) { …
SolarSync
  • 109
  • 7
-2
votes
1 answer

What is the difference between protected internal static and internal static in C#?

In my C# assembly "Abc", I have the following class and static method: internal class Xyz { protected internal static void MakeAwesome() { ... } } I noticed I can access this static method from anywhere in my assembly…
Ray
  • 7,940
  • 7
  • 58
  • 90
-2
votes
1 answer

Elegant way to create protected/abstract/etc methods in Objective-c

Objective-с doesn't have built-in method visibility like protected, private, abstract. How to implement it like in Java? Solution in my answer below
abuharsky
  • 1,151
  • 12
  • 21
-2
votes
3 answers

Why need to be a friend to access protected members?

When I'm in Sub::f() and try to access the protected members of another sub-class to which the Base* b pointer points to, it won't compile until Sub is a friend of Base. Why do I need to do this? class Base{ //friend class Sub; protected: int…
Daniel
  • 403
  • 2
  • 15
-2
votes
3 answers

The difference between private and protected

In PHP there does not seem to be a big disparity between private and protected methods. I asked why I should use protected in PHP Chatroom and got this answer: unless you have good reason not to : yes and good reason is , if you code is…
Naftali
  • 144,921
  • 39
  • 244
  • 303
-3
votes
1 answer

Make the rows non editable in Google Sheets based on the criteria of a column using Google Apps Script

I have a Google sheet which will have historical data updated by the employees. I want to freeze (non-editable) the rows having the data of Jan to July (till the previous month) and leave the current month's rows for editing. There is a column with…
-3
votes
2 answers

Understanding modifiers in Java

What is the difference between Public and Protected in Java? According to the Oracle documentation available here https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html, Public is also available to the World while Protected is not.…
-3
votes
1 answer

How does protected access modifier work and how does it differ from accessing Object class clone method

class Parent implements Cloneable{ int i ; Parent(){ } Parent(int i){ this.i = i; } public Object clonemyobj() throws CloneNotSupportedException{ return this.clone(); } protected Object cloneme()…
ALLEN
  • 1
  • 2
-3
votes
2 answers

C++ Inheritance "Ignored"

I have this block of code: struct Road_Primitive { public: Road_GPU_Point A; Road_GPU_Point B; Road_GPU_Point C; }; struct Road_Primitive_4P : public Road_Primitive { Road_GPU_Point D; }; struct Road_Primitive_3P : public…
RatkinHHK
  • 27
  • 1
  • 4
-3
votes
4 answers

How do i change a variable of another class?

I have a variable in the class "MainActivity" with the name modeNr, it is protected so it should be accessible from within the package, however whenever I try to alter it from a class in the same package it gives the error: "Non-static field…
NoahKr
  • 1
  • 4
-3
votes
1 answer

Why can a protected field be access from a class that is not a subtype?

I have wired problem. I have this class: public class Player { ... protected int x; } and in another test-class (which not extends Player): Player p = new Player("gfdg"); p.x = 10; System.out.println(p.x); ^ it's actually…
-4
votes
1 answer

Can we access protected variable in different package with the help of parent class

I make a variable that has been protected in the parent class but when I try to access it from subclass with the help of the parent class name I am unable to access it. As parent class and subclass are in different packages. I tried the below code,…
Aneesh
  • 1
  • 1
-4
votes
1 answer

cannot print using ostream but can by using cout?

Let this be the example code: object o1(//parameters); object o2(//parameters); object o3(//parameters); object *v[3]; using std::cout; //video output ofstream of; //save on file //let's suppose v[0]=&o1; v[1]=&o2; v[2]=&o3; for (int…
1 2 3
80
81