Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Question NO 3

Encapsulation is defined 'as the process of enclosing one or more items


within a physical or logical package'. Encapsulation, in object oriented
programming methodology, prevents access to implementation details.
Abstraction and encapsulation are related features in object oriented
programming. Abstraction allows making relevant information visible and
encapsulation enables a programmer to implement the desired level of
abstraction. Encapsulation is implemented by using access specifiers.
An access specifier defines the scope and visibility of a class member.
C# supports the following access specifiers −

 Public
 Private
 Protected
 Internal
 Protected internal
 Technically in encapsulation, the variables or data of a class are
hidden from any other class and can be accessed only through any
member function of own class in which they are declared.
 As in encapsulation, the data in a class is hidden from other classes,
so it is also known as data-hiding.
 Encapsulation can be achieved by: Declaring all the variables in
the class as private and using C# Properties in the class to set and
get the values of variables.

using System;
using System.Text;
 
namespace Tutlane
{
    class User
    {
        private string location;
        private string name;
        public string Location
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
            // set accessor will invoke
            u.Name = "Suresh Dasari";
            // set accessor will invoke
            u.Location = "Hyderabad";
            // get accessor will invoke
            Console.WriteLine("Name: " + u.Name);
            // get accessor will invoke
            Console.WriteLine("Location: " + u.Location);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Public Access Specifier


Public access specifier allows a class to expose its member variables and
member functions to other functions and objects. Any public member can
be accessed from outside the class.

Private Access Specifier


Private access specifier allows a class to hide its member variables and
member functions from other functions and objects. Only functions of the
same class can access its private members. Even an instance of a class
cannot access its private members.
using system;  
public class Department {  
    private string departname;.......  
    // Accessor.  
    public string GetDepartname() {  
        return departname;  
    }  
    // Mutator.  
    public void SetDepartname(string a) {  
        departname = a;  
    }  

public static int Main(string[] args) {  
    Department d = new Department();  
    d.SetDepartname("ELECTRONICS");  
    Console.WriteLine("The Department is :" + d.GetDepartname());  
    return 0;  
}  

You might also like