- Home /
What is the difference between private and protected?
I saw someone explaining and all I got from him is this:
protected; Allow a member item to only be accessed from internal or derived source.
private; Allow a member item to only be accessed from its owner.
can someone explain in more detail please
preferably in CS code.
thanks in advance
The original class cannot access it's own protected variable, it's only for childs (inherited classes) , unless you instantiate from a non-static , this case make an error
Like this :
class A
{
protected int x = 123;
private int y = 111;
}
class B : A
{
class instanceMe
{
var a = new A();
}
static void Main()
{
var a = new A();
var b = new B();
var c = new instanceMe();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
a.y=1; //OK because it's Private A
c.a.x = 10; //THIS ONE WORK because A is instantiate into another class who s not static and depend of B.
// OK, because this class derives from A.
b.x = 10;
//b.y ERROR, this one can only be accessed by instantiation of A
classes.
}
}
Answer by ArkaneX · Jan 06, 2014 at 01:49 AM
public class ClassA
{
private string text1;
protected string text2;
public ClassA()
{
text1 = "aaa"; // ok
text2 = "bbb"; // ok
}
}
public class ClassB : ClassA
{
public ClassB()
{
text1 = "aaa"; // compile error - you can't access text1, because it is visible only in scope of ClassA
text2 = "bbb"; // ok - text2 is declared as protected in ClassA, so it is accessible in ClassA and all the classes derived from ClassA
}
}
You can read more on this MSDN page
You made an error, the original class cannot access it's own protected variable, it's only for childs (inherited classes)
Like this :
class A
{
private int y = 111;
protected int x = 123;
}
class B : A
{
static void Main()
{
var a = new A();
var b = new B();
// Error CS1540, because x can only be accessed by
// classes derived from A.
// a.x = 10;
a.y=1; //OK because it's Private A
// OK, because this class derives from A.
b.x = 10;
//b.y ERROR, this one can only be accessed by instantiation of A
classes.
}
}
What you said makes no sense. Yes in your case you're using a reference to A and class B can't access the protected field of the A instance. However the class B can access its own inherited field from class A. In your example you have a completely different situation. Why don't you try the actual code that was provided in this answer? It's the same situation as your second "b" case which does work. So noone made an error here. It's all correct.
It seems you got confused about scopes here and where a certain variable actually belongs to.
Answer by gfoot · Jan 06, 2014 at 02:00 AM
"private" means that only methods of this class can access the member. "protected" means that methods of this class, or of derived classes, can access the member.
So, a base class can define fields and methods which are not accessible by users of the class, but which are accessible by implementors.
The ownership distinction is rather crude and weak - any instance of the base class can access the private members of any instance of the base class (not only itself), and any instance of the base or any derived class can access the protected members of any other instance of the base or (potentially another) derived class. So it isn't really very "protected" at all.
public class Shape
{
private List<Vector2> Points = new List<Vector2>();
protected void AddPoint(Vector2 point)
{
Points.Add(point);
}
public float CalculateArea()
{
// calculate area based on 'Points' array - it's ok to access it from
// the class that defines it
float area = 0;
int j = Points.Count - 1;
for (int i = 0; i < Points.Count; ++i)
{
area += (Points[i].x - Points[j].x) * (Points[i].y + Points[j].y) / 2;
j = i;
}
return area;
}
}
public class Square : Shape
{
public Square(float width, float height)
{
// we can't access the private Points list here, but we can access the protected AddPoint method as we're in a derived class
AddPoint(new Vector2(0, 0));
AddPoint(new Vector2(width, 0));
// etc
}
}
public class SomeComponent : MonoBehaviour
{
public void Start()
{
var shape = new Square(10.0f, 10.0f);
// CalculateArea is public so can be accessed from anywhere
Debug.Log(shape.CalculateArea());
// we can't access the Points list here, as it's private, and neither
// can we access AddPoint, as it is is protected and the calling method is
// not in a class derived from Shape
}
}
Your answer
Follow this Question
Related Questions
protected static in Unity CS? 1 Answer
Explanation on how to use functions, variables from other files 1 Answer
Private variable losing its value on editor script 1 Answer
How to keep private variables, while being able to access them in the custom EditorWindow. 2 Answers
Space Craft controller. 1 Answer