Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.
Note that only members (varaibles, properties, and methods) of a class can be assigned access specfiers (like public, private, protected). You cannot set access modifiers on local variables.
Syntax to delare a member variable:
public class Rectangle
{
private double length;
private double breadth;
protected bool isDrawn;
public Rectangle(double length, double breadth)
{
this.length = length;
this.breadth = breadth;
}
public double Length
{
get { return length; }
}
public double Breadth
{
get { return breadth; }
}
public virtual void Draw()
{
// draw logic goes here
isDrawn = true;
}
}
In the above example isDrawn is a protected variable. This means that another class working on an object of Rectangle cannot access it. It can only access its public members as shown below:
Rectangle rect = new Rectangle(30.4,12.25);
Console.WriteLine("Length: " + rect.Length); // prints this -> Length:30.4
Console.WriteLine("Breadth: " + rect.Breadth); // prints this -> Breadth:12.25
rect.Draw();
Using the following would result in compile time errors:
rect.length; // member length is private
rect.breadth; // member breadth is private
rect.isDrawn; // member isDrawn is protected
Protected members of a base class can be accessed by methods in derived classes. Example:
public class DottedLineRectangle : Rectange
{
public DottedLineRectangle(double length, double breadth)
: base(length, breadth)
{
}
public override void Draw()
{
// dotted line rectangle specific drawing logic
isDrawn = true; // Can access this as it is protected in Rectangle
}
}
As seen above a method (Draw) of the derived class (DottedLineRectangle) can access isDrawn as it is protected in the base class (Rectangle). However private members i.e. length and breadth as they are private.
Like for Reactangle, another class working on an object of DottedLineRectangle cannot access the private and protected members of Rectangle (i.e. length, breadth, and isDrawn) but can access the public members of Rectangle as shown below:
DottedLineRectangle dottedLineRect = new DottedLineRectangle(40.0,15.0);
Console.WriteLine("Length: " + dottedLineRect.Length); // prints this -> Length:40.0
Console.WriteLine("Breadth: " + dottedLineRect.Breadth); // prints this -> Breadth:15.0
dottedLineRect.Draw();
Now you can change isDrawn and make it public but that would leak it to the outside world as any other class working on an object of Rectangle can modify it. This can cause some of the code in a method of Rectangle to produce undesirable results because it was unexpectedly modified by an outside class.
If you make it private then derived class such as DottedLineRectangle would not be able to access it. As a result it would be difficult for DottedLineRectangle to indicate when it was drawn and again cause unexpected behavior because it was not modified/updated when it was expected of DottedLineRectangle to modify/update isDrawn.
The bottomline is you follow this rule:
Make all members private first and then change them to be protected or public as required for your logic to work. Ensure that you do not leak members to other classes unless when it is expected.
Firstly thank you for inviting me to Answer , i think Daanish Rumani's Answer is technically Very Good , in the Application side i think that Ahmad Anbari's answer is Nice one
they make me have nothing to say ,Really
Go Simple:
public class example01
{
protected int protected_variable;
private int private_variable01;
}
You cannot change it , if it's written by someone else. But obviously , if you want to change it to public or private yourself and you wrote the class yourself then you can change it. Protected variables can be used in inhertience also for example
public class example02 : example01
{
private int private_variable02;
public int public_variable;
public void method01example()
{
this. protected_variable =3600;
// but you cannot access the private variable, that is private_variable01;
// so if you want to change the protected variable to public or private just change the access //modifier where you declared it//See the above two explanations.
}
}
IF YOU WANT A MORE CLEAR EXAMPLE. JUST COPY PASTE THE CODE BELOW IN A CONSOLE APPLICATION USING C# AND PLAY WITH THE CODE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Examples
{
class Program02:Program //inheritence
{
public void method02()
{
this.abc =100;
Console.WriteLine(this.abc);
Console.ReadLine();
}
}
class Program // class Program
{
protected int abc; // You can change it to public or private or what ever you want.
static void Main(string[] args)
{
Program02 obj2 = new Program02();
obj2.method02();
}
}
}