Showing posts with label encapsulation. Show all posts
Showing posts with label encapsulation. Show all posts

Saturday, April 10, 2021

What is difference between Abstraction and Encapsulation?

Abstraction is about showing what is necessary to the client

Encapsulation is about hiding the complexity

Key Differences

  • Abstraction is about to show the only necessary, Encapsulation is about hiding complexity 
  • Abstraction is focusing on what to be done, Encapsulation is focusing on how it can be done.
  • Abstraction solves problems at the design level while Encapsulation solves problems at the implementation level
  • Abstraction can be achieved using Interface or Abstract class, Encapsulation is achieved through Access Modifiers (Private, Public, Protected, etc.)
  • Abstraction is to be achieved during the design level, while Encapsulation is achieved at the implementation level.

Importance of Abstraction

  • Abstraction imposes simplicity in the code
  • It hides the irrelevant details
  • Programmed can be partitioned into a different level and helps decouple the architecture
  • Provides flexibility across different types of objects

Importance of Encapsulation

  • Makes code more manageable and easy to change with new requirements
  • Makes coding easier by hiding the complexity to the client
  • This makes unit testing easy
  • It helps to change part of code rather than changing everything 
  • Makes code more readable and understandable to the client what it offers 


Tuesday, March 30, 2021

Access Modifiers - Encapsulation

We have seen Private, Public in our previous examples or posts. These are access modifiers now let's understand different types of Access Modifiers. 

Access Modifiers helps to achieve encapsulation,

Public             Accessible to all classes
Private           Only accessible to the same class
Protected       It is accessible within the same class and its inherited classes 
Internal           It is accessible within its own assembly, but not from other assembly

We will be going in details to upcoming topic of Inheritance where in broad level we can see the usage of given access modifiers

Class Car
{
        private string color; 
        public string make";
}

Class Program
{
    static void Main(string[] args)
    {
            Car myCar = new Car();
            myCar.color = "Red";      //This will throw error of inaccesible due to its protection level
            myCar.make = "Audi";    //This is allowed
    }
}

Encapsulation can be achieved through modifiers, means you can hide sensitive information from clients. 

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...