Contenido del Curso
C# Beyond Basics
C# Beyond Basics
Common Access Modifiers
You might already be familiar with the keyword public
. It is an access modifier which defines where a certain class, method or a field can be accessed from.
Similarly there are terms like private
and protected
. Both of these terms can be used to specify the accessibility of a method or a field.
When we write public
before a class, it simply makes the class accessible from different parts (files) of the program. We don't need to explore that aspect in detail as it is out of the scope of this chapter.
When we specify a method or a field as public
, we basically make that property of the class accessible from anywhere inside the code through an object:
index
using System; public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } class ConsoleApp { static void Main() { Point p = new Point(7, 9); // We can directly access 'public' fields of an object using the dot '.' symbol Console.WriteLine(p.x); Console.WriteLine(p.y); } }
If we don't want a field or a method of an object to be accessible outside the class, we can simply make it private
:
index
using System; class Point { public int x; public int y; private double magnitude; public Point(int x, int y) { this.x = x; this.y = y; this.magnitude = calculateMagnitude(); } // A private method can only be used inside the class. // Trying to use a private method outside the class might result in a compilation error. private double calculateMagnitude() { // Math.Sqrt() is the square root function. return Math.Sqrt(x * x + y * y); } // Sometimes, to be able to access data from a private field, we make public methods // which specifically retrieve the value of that field. Such methods are called "getters", // and such fields are called "read-only" fields as we can access their data but cannot modify it from outside the class. public double getMagnitude() { return magnitude; } } class ConsoleApp { static void Main() { Point p = new Point(7, 9); // Correct Console.WriteLine(p.getMagnitude()); // Error Console.WriteLine(p.calculateMagnitude()); // Error Console.WriteLine(p.magnitude); } }
It is important to note that the private fields and methods are not inherited by the derived classes:
index
using System; class Point { public int x; public int y; private double magnitude; public Point(int x, int y) { this.x = x; this.y = y; this.magnitude = calculateMagnitude(); } private double calculateMagnitude() { // Math.Sqrt() is the square root function. return Math.Sqrt(x * x + y * y); } public double getMagnitude() { return magnitude; } } class Point3D : Point { public int z; // The 'base(x, y)' part simply calls the constructor of the 'Point' class with 'x' and 'y' values. // This will be more thoroughly explained in the later section. public Point3D(int x, int y, int z) : base(x, y) { this.z = z; // Error calculateMagnitude(); } } class ConsoleApp { static void Main() { Point3D p = new Point3D(5, 7, 9); // Correct according to rules of language, however note that it will give the magnitude of x and y excluding z (for a 2D point) so logically the result will be wrong Console.WriteLine(p.getMagnitude()); } }
To make a field or method inaccessible from outside the class but accessible from child classes, we use the protected
keyword:
index
using System; class Point { public int x; public int y; protected double magnitude; public Point(int x, int y) { this.x = x; this.y = y; this.magnitude = calculateMagnitude(); } protected double calculateMagnitude() { // Math.Sqrt() is the squareroot function. return Math.Sqrt(x * x + y * y); } public double getMagnitude() { return magnitude; } } class Point3D : Point { public int z; public Point3D(int x, int y, int z) : base(x, y) { this.z = z; // No Error Now calculateMagnitude(); } } class ConsoleApp { static void Main() { Point3D p = new Point3D(5, 7, 9); Console.WriteLine(p.getMagnitude()); } }
Sometimes, to be able to access private attributes from a class we create public methods called getters. The method getMagnitude
is an example of a getter. This ensures that the field is only readable and not writable, making that attribute read-only.
Note: If no access modifier is specified to a class member, it is assumed to be private
by default.
¡Gracias por tus comentarios!