Topic : Data Abstraction in C# Programming language:

Data Abstraction:

Object Oriented Programming has a special Feature called Data Abstraction. Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e, to represent the needed information in program without presenting the details.

Abstraction shows only important things to the user and hides the internal details. For Example: When we use our Laptop, We only know how to use it, but we don't know Exactly how it's work. in another side, when our Laptop is Locked, the user can't see our personal Information on the laptop. so when he will put the password and unlock the windows then he'll be able to access the information. so this window password work like Abstraction.


image: Data-Abstraction

Let's take another real-life example of Data Abstraction to clear the concept. Let's take an example of a Laptop, Which we can Turn on and shutdown, we can use the internet, or watch Movies and listen to a song. We can add external components, like extra keyboard, USB Drive, or Extra Hard disk, We can Increase and decrease the volume while listening music. and most important, we are coding using our Laptop. But we don't know how exactly the Laptop Works, we don't know it's Internal Methods. we don't know how it connects us with the Internet using WiFi or DSL. And you don't know how you actually access infobrother through just Cable or WiFi. This is all about Data Abstraction. Laptop Abstract it's Internal Information from the user.



Access Labels Enforce Abstraction:

In C#, We use Access labels to define the abstract Interface to the Class. A Class may contain zero or more Access Labels:



Access LabelDefinition:
Public:The class Member that is defined as Public can be accessed by other class Member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace.
Private:The Class member that is defined as Private, can't be accessed from outside the class. The Private access specifiers restrict the member variable or method to be called outside from the parent class.
Protected:The Protected access specifier hides its member methods and variables from other classes. This type of variable or methods can only be accessed in a child class. if we want to inherit some properties of a method from one class to another, then we declare our method or variable as Protected:
Internal:The Class member that is defined as internal, can be access within the Program that contain its declarations. we can access any internal declared member within the same namespace that contains its declaration. but its hide from other classes that resides in another namespace. it is a default, its mean if we didn't mention any access specifier, then "internal" will kick in:
Protected Internal:The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.


Let's we have an simple example to know, how these Access Label help us to Abstract our personal data from outside the world.


/*Example: Data Abstraction : InfoBrother:*/

using System;

namespace Abstraction
{
    class game
    {
        private int number = 15;  //private member:
        public int attempt;

        public void guess() //public method :
        {
            Console.WriteLine("Enter your Guess: ");
            do //repeat this statement:
            {                
                attempt = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Guess Again: ");
            }
            while (attempt != number);  //until user enter 15;
            Console.WriteLine("That's the right guess:");
        }
    }


    class Program    //our Main class:
    {
        static void Main(string[] args)  
        {
            game Game = new game();  //Object of Class game:
            Game.guess();  //calling method:


            Console.ReadKey();
        }
    }
}


In our above Example, we just create an simple Guess Game, we hide a number in Our program using "Private" keyword. no one can see this number from outside the program. It's mean our program provide only excential information to User. and abstract the secret information that is number from the user.



Advantages of abstraction:

The Advantages of abstraction are the hiding of implementation details, component reuse, extensibility, and testability. When we hide implementation details, we reveal a cleaner, more comprehensible and usable interface to our users. We are separating our interface from our implementation, and this makes component reuse more practical. Many, if not all of the object-oriented concepts we have discussed throughout this document play a role in the abstraction principle. Working together, their end goal is the same, to produce software that is flexible, testable, maintainable, and extensible.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement