Tuesday, 28 June 2016

Encapsulation and Abstraction in a simple way.

I will try to demonstrate Encapsulation and Abstraction in a simple way.. Lets see..

The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Encapsulation containing and hiding information about an object, such as internal data structures and code.
Encapsulation is -

Hiding Complexity,
Binding Data and Function together,
Making Complicated Method's Private,
Making Instance Variable's Private,
Hiding Unnecessary Data and Functions from End User.
Encapsulation implements Abstraction.

And Abstraction is -

Showing Whats Necessary,
Data needs to abstract from End User,
Lets see an example-

The below Image shows a GUI of "Customer Details to be ADD-ed into a Database".

By looking at the Image we can say that we need a Customer Class.

Step - 1: What does my Customer Class needs?

i.e.

2 variables to store Customer Code and Customer Name.

1 Function to Add the Customer Code and Customer Name into Database.

  namespace CustomerContent
    {
       public class Customer
       {
           public string CustomerCode = "";
           public string CustomerName = "";
           public void ADD()
           {
              //my DB code will go here
           }
Now only ADD method wont work here alone.

Step -2: How will the validation work, ADD Function act?

We will need Database Connection code and Validation Code (Extra Methods).

     public bool Validate()
     {
    //Granular Customer Code and Name
    return true;
     }

     public bool CreateDBObject()
     {
    //DB Connection Code
    return true;
     }

class Program
{
   static void main(String[] args)
   {
     CustomerComponent.Customer obj = new CustomerComponent.Customer;

     obj.CustomerCode = "s001";
     obj.CustomerName = "Mac";

     obj.Validate();
     obj.CreateDBObject();

     obj.ADD();
    }
}
Now there is no need of showing the Extra Methods(Validate(); CreateDBObject() [Complicated and Extra method] ) to the End User.End user only needs to see and know about Customer Code, Customer Name and ADD button which will ADD the record.. End User doesn't care about HOW it will ADD the Data to Database?.

Step -3: Private the extra and complicated methods which doesn't involves End User's Interaction.

So making those Complicated and Extra method as Private instead Public(i.e Hiding those methods) and deleting the obj.Validate(); obj.CreateDBObject(); from main in class Program we achieve Encapsulation.

In other words Simplifying Interface to End User is Encapsulation.

So now the complete code looks like as below -

namespace CustomerContent
{
     public class Customer
     {
        public string CustomerCode = "";
        public string CustomerName = "";

        public void ADD()
        {
           //my DB code will go here
        }

        private bool Validate()
        {
           //Granular Customer Code and Name
           return true;
        }

        private bool CreateDBObject()
        {
           //DB Connection Code
           return true;
        }

  class Program
  {
     static void main(String[] args)
     {
        CustomerComponent.Customer obj = new CustomerComponent.Customer;

        obj.CustomerCode = "s001";

        obj.CustomerName = "Mac";

        obj.ADD();
   }
}
Summary :

Step -1: What does my Customer Class needs? is Abstraction.

Step -3: Step -3: Private the extra and complicated methods which doesn't involves End User's Interaction is Encapsulation.

P.S. - The code above is hard and fast.

Thursday, 16 June 2016

Differrnce between Primary Key, Unique Key & Foreign Key

Primary Key

Primary key cannot have a NULL value.
Each table can have only one primary key.
By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
Primary key can be related with another table's as a Foreign Key.
We can generated ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value.

Unique Key

Unique Constraint may have a NULL value.
Each table can have more than one Unique Constraint.
By default, Unique key is a unique non-clustered index.
Unique Constraint can not be related with another table's as a Foreign Key.
Unique Constraint doesn't supports Auto Increment value.

Foreign Key

Foreign key is a field in the table that is primary key in another table.
Foreign key can accept multiple null value.
Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
We can have more than one foreign key in a table.
There are actual advantages to having a foreign key be supported with a clustered index, but you get only one per table. What's the advantage? If you are selecting the parent plus all child records, you want the child records next to each other. This is easy to accomplish using a clustered index.
Having a null foreign key is usually a bad idea. In the example below, the record in [dbo].[child] is what would be referred to as an "orphan record". Think long and hard before doing this.

Difference between Union and Union All

1. UNION removes duplicate rows.

“UNION ALL” does not remove the duplicate row. It returns all from all queries.

2. UNION uses a distinct sort.

“UNION ALL” does not use a distinct sort, so the performance of “UNION ALL” is slightly higher than “UNION”.

3. UNION cannot work with a column that has a TEXT data type.

UNION ALL can work with all data type columns.

Monday, 13 June 2016

Multiple Inheritance With Interfaces

Introduction

This article gives an idea about the situation called interface clash. This situation occurs when two interfaces have functions with the same name and signature and one base class implements these interfaces.

Background

In earlier languages like C++, there was the concept of Multiple Inheritance. But in C#, we do not have any such feature. This was something in the list of features which is available in C++ but not in C#. Due to this feature in C++, developers were facing the problem of Diamond Inheritance. That’s why Virtual Functions came into the picture.

Using the Code

In C#, when two interfaces have functions with the same name and a class implements these interfaces, then we have to specifically handle this situation. We have to tell the compiler which class function we want to implement. For such cases, we have to use the name of the interface during function implementation. Have a look at the following example:

Blocks of code should be set as style Formatted like this:

Ex:
    /// <summary />
    /// Interface 1
    /// </summary />
    public interface Interface1
    {
        /// <summary />
        /// Function with the same name as Interface 2
        /// </summary />
        void MyInterfaceFunction();
    }

    /// <summary />
    /// Interface 2
    /// </summary />
    public interface Interface2
    {
        /// <summary />
        /// Function with the same name as Interface 1
        /// </summary />
        void MyInterfaceFunction();
    }

    /// <summary />
    /// MyTestBaseClass Implements the two interfaces Interface1 and Interface2
    /// </summary />
    public class MyTestBaseClass:Interface1,Interface2
    {
        #region Interface1 Members

        void Interface1.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface1 Function()");
            return;
        }

        #endregion

        #region Interface2 Members

        void Interface2.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface2 Function()");
            return;
        }

        #endregion
    }

In the above example, we are implementing the function MyInterfaceFunction() by using its interface name. In this case if we create the object of MyTestBaseClass and check for MyInterfaceFunction(), it won't be directly available. Look at the following code:

MyTestBaseClass obj = new MyTestBaseClass();
//Following code would give an error saying that
//class does not have a definition for MyInterfaceFunction.
obj.MyInterfaceFunction();

We can call the respective interface function by typecasting it to the corresponding interfaces. See the example below:

//This code would work fine and calls the function of the first interface
((Interface1)obj).MyInterfaceFunction();

//This code would also work fine and calls the function of the second interface
((Interface2)obj).MyInterfaceFunction();

Now comes the twist. We will add one more function with the same name in MyTestBaseClass. Now try to access the elements of the object of MyTestBaseClass. You will see that MyInterfaceFunction is available there. See the following code now and this code would call the function of interfaces first followed by the Class function.

Ex.
    /// <summary />
    /// MyTestBaseClass implements the two interfaces Interface1 and Interface2
    /// </summary />
    public class MyTestBaseClass:Interface1,Interface2
    {
        public void MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyTestBaseClass's Function()");
            return;
        }

        #region Interface1 Members

        void Interface1.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface1 Function()");
            return;
        }

        #endregion

        #region Interface2 Members

        void Interface2.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface2 Function()");
            return;
        }

        #endregion
    }

    /// <summary />
    /// Interface 1
    /// </summary />
    public interface Interface1
    {
        /// <summary />
        /// Function with Same Name as Interface 2
        /// </summary />
        void MyInterfaceFunction();
    }

    /// <summary />
    /// Interface 2
    /// </summary />
    public interface Interface2
    {
        /// <summary />
        /// Function with Same Name as Interface 1
        /// </summary />
        void MyInterfaceFunction();
    }

In the Main Function –

MyTestBaseClass obj = new MyTestBaseClass();

//This code would work fine and calls the function of the first interface
((Interface1)obj).MyInterfaceFunction();

//This code would work fine and calls the function of the second interface
((Interface2)obj).MyInterfaceFunction();

//This code would call the class function
obj.MyInterfaceFunction();
Now comes another twist. You derive a class MyDerivedClass() from MyTestBaseClass(). Before that, remove the function last implemented in MyTestBaseClass(). Now create the instance of MyDerivedClass() and check. No function would be available with this class even though its base class is having these functions.

Ex:
    /// <summary />
    /// Interface 1
    /// </summary />
    public interface Interface1
    {
        /// <summary />
        /// Function with the same name as Interface 2
        /// </summary />
        void MyInterfaceFunction();
    }

    /// <summary />
    /// Interface 2
    /// </summary />
    public interface Interface2
    {
        /// <summary />
        /// Function with the same name as Interface 1
        /// </summary />
        void MyInterfaceFunction();
    }

    /// <summary />
    /// MyTestBaseClass implements the two interfaces Interface1 and Interface2
    /// </summary />
    public class MyTestBaseClass:Interface1,Interface2
    {

        //public void MyInterfaceFunction()
        //{
        //    MessageBox.Show("Frm MyTestBaseClass's Function()");
        //    return;
        //}

        #region Interface1 Members

        void Interface1.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface1 Function()");
            return;
        }

        #endregion

        #region Interface2 Members

        void Interface2.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface2 Function()");
            return;
        }

        #endregion
    }

    /// <summary />
    /// New Derived class which is derived from MyTestBaseClass
    /// </summary />
    public class MyDerivedClass : MyTestBaseClass
    {
        //No Functions Here....
    }
So again here for getting the function of interfaces, we have to separately typecast the object of the derived class with the interface. See the code below:

//In the Main Function……

//This code would call the Interface1 function
MyDerivedClass derivedObj = new MyDerivedClass();
((Interface1)derivedObj).MyInterfaceFunction();       

Conclusion
From the above article, you should get an idea about calling interface functions when there is a clash. Your suggestions and views will be appreciated.