Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, February 25, 2017

Abstraction and Encapsulation in OOPS


Abstraction is the process of filtering relevant information of a class.Abstraction is an approach to choose concerned information and remove non concerned information.
In OOPs ,Object support Abstraction.Object can expose internal information of a class.So,Abstraction is the inner layout of a object.

We have a Class Company.When we try to filter,what variable this company class can have  is Abstraction. Company class can have Employee,Department,Address.To choose concerned information related to Company Class is called Abstraction.

  class Company
    {
        private int Employee;
        private string Department;
        private string Address;
    }

Data Hiding
If object is able to keep his internal data safely,it is called data hiding.in simple words,any information of class,that does not directly access by object is called data hiding.

If there is no data hiding,no meaning of Encapsulation.We achieve data hiding by private keyword.

Encapsulation is a process to access private data outside class by making function or properties
.Encapsulation provides a way to protect data from accidental corruption. Rather than defining the data in the form of public, we can declare those fields as private.

Abstraction-->Data hiding-->Encapsulation
If there is data hiding,there is a way for Encapsulation.

I have created a console application.I have created a Company class . I have created 3 variable and make all private.I have created an object of company class and try to access member variable of Company class,we cannot access directly. As all member variable are private,we cannot access it directly.It is called data hiding. If we want to access member variable,we have to make a process,this process to access private data outside class is called Encapsulation. We do it by making function.We can also access by properties.

I have explained by making function. Properties is discussion of C#.It will explain it in C# tutorials.

1)  Company class

    class Company
    {
        private int Employee;
        private string Department;
        private string Address;

        public int GetEmployee()
        {
            return Employee;
        }
        public void SetEmployee(int a)
        {
            Employee = a;
        }
        public string GetDepartment()
        {
            return Department;
        }
        public void SetDepartment(string a)
        {
            Department = a;
        }

    }

2)  Program Class

class Program
    {
        static void Main(string[] args)
        {
            Company cs = new Company();
            cs.SetEmployee(100);
            Console.WriteLine("No of employee is " + cs.GetEmployee());
            cs.SetDepartment("Finance");
            Console.WriteLine("Department is " + cs.GetDepartment());
            Console.ReadLine();
            }
    }
3)Output


You also like this.
2)Inheritance in OOPS

3)Polymorphism in OOPS

4)Interface in OOPS

5)What is Virtual Function

6)What is Abstract class and Abstract function

7)What is Static Class and Static Members

2 comments :