Abstraction - DotNet and DataBase

Tuesday, 24 July 2018

Abstraction




Abstraction is the process of hiding the low priority class members and showing the required features only.
Let us suppose, we are using a mobile phone. As an end user, we should know better on outer parts like screen, battery and design. Dont need to know much about the inner parts like connections between the hard ware etc..

On the same way, while developing a class, we should show only the required features to the  users who uses our class.

For example we are creating a time sheet page. For that we need to work two things.
1)whether the user exists or not?
2)Inserting the hrs into the database.

Please see the example below
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[] userid = { 200, 300, 400 };
            int iVal = 0;
            if (userid.Contains(uid))
            {
                iVal = 1;
            }
            return iVal;
        }
        public void InsertupdateHrs(int hrs)
        {
            Console.WriteLine("Employee information updated.");
        }
    }

    class Encapsulation
    {
        Employeeinformation e = new Employeeinformation();
        void Evalidate()
        {
           int ivaliedate =e.validateuser(200);
            if (ivaliedate==1)
            {
                e.InsertupdateHrs(8);
            }
            else
            {
                Console.WriteLine("No user exists with the supplied userID.");
            }
        }
        static void Main()
        {
            Encapsulation e = new Encapsulation();
            e.Evalidate();
            Console.Read();
        }
    }
}

In the above example Employeeinformation  contains two methods  validateuser() and InsertupdateHrs(). Here validateuser() checks whether the user exists or not and  InsertupdateHrs() inserts the hours into the database.

The user who access this class need to use two methods to insert hours. To minimize the user efforts we can write the  code as below,
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 and called under validateuser() method. So, it is not accessible (hided) to the user out side the class but it is invoked via validateuser() method.

This is called abstraction.




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.