Sunday, 8 April 2018

Abstraction in .Net

Encapsulation: Encapsulate hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.

Abstraction: Abstraction is used to showing what is relevant but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.

Constructor in abstract class
There's an implicit call to the base constructor prior to the derived constructor execution. Keep in mind that unlike interfaces, abstract classes do contain implementation. That implementation may require field initialization or other instance members. Note the following example and the output:

abstract class Animal
       public string DefaultMessage { get; set; }

       public Animal()
           Console.WriteLine("Animal constructor called");
           DefaultMessage = "Default Speak";
       public virtual void Speak()

    class Dog : Animal
        public Dog(): base()//base() redundant.  There's an implicit call to base here.
            Console.WriteLine("Dog constructor called");
        public override void Speak()
            Console.WriteLine("Custom Speak");//append new behavior
            base.Speak();//Re-use base behavior too

Although we cannot directly construct an Animal with new, the constructor is implicitly called when we construct a Dog.

Animal constructor called
Dog constructor called
Custom Speak
Default Speak

IN Short :
Encapsulation: hiding data using getters and setters etc.
Abstraction: hiding implementation using abstract classes and interfaces etc.


  1. this post "abstraction in net was very usefull for me and friends, nice post

  2. Nice Article,Good information,keep sharing.



ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)