Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Sunday, April 15, 2018

[C#]: Explicit Interface and Implicit Interface In C# With Example [Step by Step]

In this post, we will learn the types of Interface in C#.NET with an example in a simple way.
We have already discussed the basic concept of the interface in part one of this series. If you want to learn please click on the link basic interface concept.
Interface In C# With Example

Type of interface in C#
  • Explicit Interface
  • Implicit Interface 
  1. Explicit Interface:
    If the class implement two interfaces that contain a member with the same signature.to implementing those interface member in class we take help of explicit interface.
How to implement the explicit interface?
  1. return_Type Interface_Name.Method_Name()  
  2.         {  
  3.             //...  
  4.         }  

Note: The modifier is not valid to implement an explicit interface. 

Example 1:
Create two interfaces having only one method with the same signature as follow. 
  1. interface IVendor  
  2.    {  
  3.        void GetVendor();  
  4.    }  
  5.    interface ISupplier  
  6.    {  
  7.        void GetVendor();  
  8.    }  

Create one class name like 'UserService' to implementing those interfaces. 
  1. public class UserService:IVendor,ISupplier  
  2.    {  
  3.           
  4.         void IVendor.GetVendor()  
  5.        {  
  6.            Console.WriteLine("GetVendor() method called from IVendor interface");  
  7.        }  
  8.         void ISupplier.GetVendor()  
  9.        {  
  10.            Console.WriteLine("GetVendor() method called from ISupplier interface");  
  11.        }  
  12.    }  

 If you try to implement the interface member without 'interface name', then it will through an error as follows.

 Interface In C# With Example

How to call the interface member?

    In the explicit interface, we can not call the interface member directly using the object of the class. we have two way of calling explicit interface as following.

There is two way of calling an explicit interface member: 
  1. The first way to creating the instance of the class and typecast the interface type.
  2. Second Way creating an object of the class and reference variable of the interface type.
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            // The first way to creating the instance of the class and typecast the interface type.   
  6.            UserService userService = new UserService();  
  7.            ((IVendor)userService).GetVendor();  
  8.            ((ISupplier)userService).GetVendor();  
  9.   
  10.            // Second Way creating an object of the class and reference variable of the interface type.  
  11.   
  12.            IVendor vendor = new UserService();  
  13.            vendor.GetVendor();  
  14.            ISupplier supplier = new UserService();  
  15.            supplier.GetVendor();  
  16.   
  17.            Console.ReadLine();  
  18.        }  
  19.    } 

 Output: 
 Interface In C# With Example

You can use either one of the ways of calling an explicit interface member as per the requirement.
Example 2:

  1. using System;  
  2.   
  3. namespace CSInterfaceDemo.Customer  
  4. {  
  5.     interface IService1  
  6.     {  
  7.         void Print();  
  8.     }  
  9.     interface IService2  
  10.     {  
  11.         void Print();  
  12.     }  
  13.     public class Customer:IService1, IService2  
  14.     {  
  15.         void IService1.Print()  
  16.         {  
  17.             Console.WriteLine("IService1.Print()");  
  18.         }  
  19.         void IService2.Print()  
  20.         {  
  21.             Console.WriteLine("IService2.Print()");  
  22.         }  
  23.     }  
  24.   
  25.     class program  
  26.     {  
  27.         static void Main(string[] args)  
  28.         {  
  29.             // The first way to creating the instance of the class and typecast the interface type.     
  30.             Customer customer = new Customer();  
  31.             ((IService1)customer).Print();  
  32.             ((IService2)customer).Print();  
  33.   
  34.             // Second Way creating an object of the class and reference variable of the interface type.   
  35.             IService1 service1 = new Customer();  
  36.             service1.Print();  
  37.             IService2 service2 = new Customer();  
  38.             service2.Print();  
  39.   
  40.             Console.ReadLine();  
  41.         }  
  42.     }  
  43. }  

    
2. Implicit Interface: The implicit interface does not have the same signature of the interface member. We can call the implicit interface member by using the object of the class.

Note: The modifier is required to implement the implicit interface.

 How to implement the implicit interface?

  1. modifier return_type interface_method_name()  
  2.         {  
  3.             //....
  4.         }  

The following is an example of the implicit interface.

 Example 1:
  1. using System;  
  2.   
  3. namespace CSInterfaceDemo.Customer  
  4. {  
  5.     interface IService1  
  6.     {  
  7.         void Print();  
  8.     }  
  9.     interface IService2  
  10.     {  
  11.         void Display();  
  12.     }  
  13.     public class Customer:IService1, IService2  
  14.     {  
  15.        public void Print()  
  16.         {  
  17.             Console.WriteLine("IService1.Print()");  
  18.         }  
  19.        public void Display()  
  20.         {  
  21.             Console.WriteLine("IService2.Display()");  
  22.         }  
  23.     }  
  24.   
  25.     class program  
  26.     {  
  27.         static void Main(string[] args)  
  28.         {  
  29.             Customer customer = new Customer();  
  30.             customer.Print();  
  31.             customer.Display();  
  32.   
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  

 Note :
The best practice to work with the interface by adding new code. You can achieve a new requirement by adding new separate interface instead of changing the old source code. The same advice from the Interface Segregation Principle (ISP),
“Clients should not be forced to depend on methods that they do not use.” by Robert C. Martin.

 I hope you understood the basic concept of explicit and implicit interface.

More information watch video as follows:

Share:

Friday, April 13, 2018

Abstraction And Encapsulation In C#

In this tutorial, we will learn data Abstraction and Encapsulation in C# and I will provide real-time use of Data Abstraction and Encapsulation with the example in a simple way. I hope this will very helpful for you to develop the real-time project for fresher as well as experienced people.
Abstraction And Encapsulation In C#


What is the need for Data Abstraction?
If you want to hide your business logic from the outside world. To achieve this implementation we can use Data Abstraction.

What is Data Abstraction?:
It is used to provide the relevant information and hide the irrelevant information from the user it is called as data abstraction.
It will only expose the relevant information to the outside world. It will provide the necessary information to the user and hide unnecessary information from the user.

Example:
  1. Console.WriteLine();  

This is the example of data abstraction. The "Console.WriteLine()" method is provided by Microsoft and they have written a lot of code behind the "WriteLine()" method but, they don't show all the code to the user. They only provide what the user want means they provide only necessary information to the user. and hide unnecessary information from the user. The user doesn't need what code written behind the "WriteLine" method. so that, they expose the "WriteLine()" method to the outside world. The user can access easily. This is the concept of data abstraction.

Example 2:
real time example of abstraction in c#

TV or Car remote is the also example fo Abstraction.TV or Car remote is assembled from the collection of circuits but they don't show to the user all circuits behind the remote, They only provide remote to the user to use it. When the user presses the key on remote the channel gets changed. They provide only necessary information to the user. This concept is called Data Abstraction.

What is the need for Data Encapsulation?

If you want to hide the complexity and provide the security to data. To achieve this implementation the oops concept came i.e. Data Encapsulation.

What is Data Encapsulation?
It is used to bind or group the related data together, and hide the complexity, and provide the security for the data. It is called Data Encapsulation.

Non-Technical Example:
Abstraction And Encapsulation In C#
A capsule is the best example of encapsulation. A capsule is inside the wrapper and wrapper provide the security to the capsule and wrapper is work as a container like class.

Example:
  1. public class AbstractionAndEncapsulation  
  2.   
  3. {  
  4.   
  5.  // Class member declaration and definition here  
  6.   

The class is the best example of Encapsulation. It will encapsulate or put everything into one thing and provide others to use it. And also It will hide the complexity and provide the security for data.
We can say in a non-technical term Like a TV or Car remote. It assembled from the collection of circuits but they hide all circuits from the user. They encapsulate all circuits in one thing called remote and provide to the user to use it. And also remote can provide security to all circuits used inside the remote. This concept called Encapsulation.
I hope you now have a basic idea about both of these oops concepts. Let's see a real-world example of encapsulation and abstraction. 
Let's assume you have to create "AddUserDetail()" method to insert users data and pass it to other developers to use. So first, create a class named as "AbstractionAndEncapsulation" and "SaveUserDetailToDB()" a method to insert the data into the database. Create one more method for user validation named as "ValidateUser()"

Real-Time Use Of Abstraction and Encapsulation:

  1. public class AbstractionAndEncapsulation  
  2.    {  
  3.        public bool AddUserDetail(string name, string email)  
  4.        {  
  5.            if (ValidateUser(name, email))  
  6.            {  
  7.                if (SaveUserDetailToDB(name, email) > 0)  
  8.                {  
  9.                    return true;  
  10.                }  
  11.            }  
  12.            return false;  
  13.        }  
  14.        private bool ValidateUser(string name, string email)  
  15.        {  
  16.            // This method is used to validate the user detail and return true or false based on result.  
  17.            return true;  
  18.        }  
  19.        private int SaveUserDetailToDB(string name, string email)  
  20.        {  
  21.            // Write the code to save the data into Database  
  22.            return 1;  
  23.        }         
  24.    }  

As you can see, there are three methods that are written in above class.
  • AddUserDetail: This method can call from outside the class. It is a public method.
  • ValidateUser: To validate the user detail. This method can not call from outside the class. It is a private method.
  • SaveUserDetailToDB: To insert user data into database table. This method can not call from outside the class. It is a private method.
Now another user can call just "AdduserDetail()" method which is public. and another method cannot be accessed outside the world or class.
It means that the class AbstractionAndEncapsulation which we have created in the above example that achieves two concepts like Abstraction and Encapsulation. It will provide relevant information to the user. and It also groups the related data, and hide complexity, and provide security for the data. To call the "AddUserDetail()" method, do as follows:

  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           AbstractionAndEncapsulation objUser = new AbstractionAndEncapsulation();  
  6.           bool isResult = objUser.AddUserDetail("Shri""shri@gmail.com");  
  7.           Console.WriteLine();  
  8.       }  
  9.   }  

Now come back to the main discussion.
Here, we are hiding the procedure of adding data into a database from other users, this is Abstraction. And putting all the three methods into one class and providing other users to use it, that is called Encapsulation.
  • We have two methods private so that, It does not access to other classes and also putting all the three methods into one class called as "AbstractionAndEncapsulation", So we can say we have achieved Encapsulation.
  • We have provided only "AddUserDetail()" method to the user, User doesn't know what we are doing with this method so we can say, We have achieved Abstraction here.
I hope you have cleared your doubts about the concepts of Encapsulation and Abstraction.

Following are the previews article on OOPs



References:

More Detail with real-time example visit following link:


Share:

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave