Encapsulation - DotNet and DataBase

Thursday, 2 August 2018

Encapsulation


Encapsulation is the process of hiding unnecessary object outside the class. Encapsulation is implemented by using access specifiers. An access specifier restricts the scope and visibility of a class member.
In a class, if we don't want to disclose the objects outside the class, we have to create the objects with private access modifier. If it is an assembly and doesn't want to access outside the assembly, objects have to create with internal access modifier.

Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritenceDiscussion
{
    class Employeeinformation
    {
        public int validateuser(int uid,int hrs)
        {
            int[] userid = { 200,300,400 };
            int iVal = 0;
            {
                if(userid.Contains(uid))
                {
                    InsertupdateHrs(hrs);
                }               
            }
            return iVal;
        }
        private void InsertupdateHrs(int hrs)
        {
            Console.WriteLine("Employee information updated.");
        }
    }

    class Encapsulation
{
        Employeeinformation e = new Employeeinformation();
        void Evalidate()
        {
           e.validateuser(200,8);
        }
        static void Main()
        {
            Encapsulation e=new Encapsulation();
            e.Evalidate();
            Console.Read();
        }
    }
}

Here I made InsertupdateHrs() method private. So, it is not accessible (hide) to the user outside the class.

This is called encapsulation.



No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.