Sunday 10 August 2014

Encapsulation in C#

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


Here i have shows the use of Private access specifier

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

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
Encapsulation c = new Encapsulation ();
c.getdetails();
c.displaydetails ();
              Console.ReadLine();
        }

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

namespace Practice
{
    class Encapsulation
    {
        private int hieght;
        private int width;
        public void getdetails()
        {
            Console.WriteLine("please enter hieght");
            hieght = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("please enter width");
            width = Convert.ToInt32(Console.ReadLine());
        }
        public double getarea()
        {
            return hieght * width;
        }
        public void displaydetails()
        {
            Console.WriteLine("hieght is {0}",hieght);
            Console.WriteLine("width is {0}",width);
            Console.WriteLine("area is {0}", getarea());
        }
    }
}

No comments:

Post a Comment