The protected access modifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Protected_Modifier { class ProtectedClass { // String Variable declared as protected protected string ComputerName; public void print() { Console.WriteLine("\nMy name is " + ComputerName); } }
class MainClass : ProtectedClass // Inherit access class { static void Main(string[] args) { MainClass p = new MainClass(); Console.Write("Enter your CounputerName:\t"); p.ComputerName = Console.ReadLine(); // No Error!! p.print(); Console.ReadLine(); } } }
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The protected access modifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.