Abstraction - dotnet and db

Follow us on Facebook

SQL Server Mountain View Mountain View Mountain View

Tuesday, July 24, 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.




8 comments:

  1. Thanks for the interesting blog that you have implemented here. Very helpful and innovative. Waiting for your next upcoming article.
    Digital Marketing Course In Kolkata

    ReplyDelete
  2. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. great work
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete

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.