Learn how to provide necessary information to the users and hide the unnecessary details using C# programs.

The concept of “providing necessary details to the clients/users and hiding the unnecessary details” is called Abstraction.

As an example consider a login page where username and password is enough for the users. so, only username and password fields are shown in the login page and validation and database connection should be hidden from the users or clients.

Let’s understand the concept of abstraction by implementing above scenario with example programs.

There is a class LoginPage that will allow client class or main() program to create object of its own with username and password information and submit() method only and rest of information e.g. internal methods like VerifyCredential() and DBConnection class will be abstracted from client class.

Below is the C# program to implement the abstraction concept.

C# code:

//Login window class where 

using System;
 //Login window class where 
    class LoginPage 
    {
        /*--------------Internal purpose only-----------
	 * Internal private methods variable and classes
	 * Not visible or accessible out side of login Page
	 * class.
	 * 
	 */
        private String userName;
        private String password;
        private DBConnection connection;

        private void VerifyCredential()
        {

            this.connection = new DBConnection();
            this.connection.DBConnect();
            this.connection.verifyUser(this.userName, this.password);

        }
        /*-------------------------------------------------
	 * Only methods visible to User/Client program.
	 * Client/User can be main() program or other classes
	 */
        public LoginPage(String userId, String pwd)
        {
            this.userName= userName;
            this.password= password;
        }

        public void submit()
        {
            VerifyCredential();
        }


       

       
       
        private class DBConnection
        {

            public void DBConnect()
            {

                Console.WriteLine("Connecting Database...");

            }

            public void verifyUser(String UserId, String pwd)
            {
                Console.WriteLine("Verifing user...");


            }

        }
    }

    /*------------------------------------------
 * Client class or main function  
 */
    public class Abstraction
    {

        public static void Main(String[] args)
        {

            // Put user id and password to login
            LoginPage lp = new LoginPage("User1", "pwd123");
            lw.submit();

        }

    }

NOTE:

In above abstraction concept, we have provide only required information to main() program and have hide the information e.g. internal methods and classes.

To hide the complexities we have adapted some mechanism in above program e.g. we have hide username , password variable, data base class and VerifyCredential() methods by making them private using private modifiers

The way of hiding complexities / information are known as encapsulation. In oops whatever way you can apply to hide information, all are known as encapsulation.

Read another C# interview question What is encapsulation in C# object oriented programming

As a final note,

Abstraction in C#: Provide only necessary information to client.

Encapsulation: Whatever the way we apply to hide information is encapsulation.

“In fact, implementation of encapsulation is Abstraction”

Related Posts