locked
Difference between encapsulation and abstraction RRS feed

  • Question

  • User583311198 posted

    Hi,

    I always get confused with encapsulation and abstraction. I want to know a descriptive and real time difference between these two.

    Friday, July 18, 2014 2:28 PM

Answers

  • User281315223 posted

    What they are :

    Abstraction is "the process of identifying common patterns that have systematic variations. Abstract classes provide a way to force an inherited class to impliment an override method, similar to, but not the same as an interface. Unlike an interface, abstract classes are fully functional classes, but you can force one or more methods to be overridden.

    Encapsulation is the ability for an object to hide its data and methods from those who do not need to know, and only expose data and methods that are required. With other words, is the process of protecting the data using properties, so that any attempt to modify the data is done through the proper channels, ideally with an audit log for the bank account class.

    Encapsulation

    Encapsulation is the concept of "information or data hiding" and it focuses primarily on hiding the necessary implementation details and it can also be the concept that handles "grouping" different properties, methods and functions into a single object. You'll see this quite often within C# with the use of classes and structures, which "encapsulates" one or more properties, methods, functions, behaviors into a single object (a class in many cases) and uses this class to function as a single logical unit : 

    public class Widget
    {
        private string WidgetID {get; set;}
        
        public void TurnOn()
        {
             // Do something here
        }
    
        public void TurnOff()
        {
             // Do something here
        }
    }

    The above class can be an example of encapsulation as it wraps a single property and two methods within it and functions as a single object. This can be used to not only to bundle data, but also to restrict access through access modifiers (public, private, internal, etc.) and other approaches.

    Abstraction

    Abstraction is a bit similar to encapsulation in the sense that it also allows you to "hide" things, but it does it in a different way. Where encapsulation above would allow you to hide actual data and properties, abstraction allows you to hide implementation details and generalize what an object or class can do. You'll commonly see abstraction being used in both interfaces and abstract classes as it will essentially provide a blueprint for a class, but it will not have any specific implementation details.

    For instance, you might have an abstract class called an Animal and the animal might make a noise as seen below :

    public abstract class Animal
    {
         // Every animal makes a noise, so they should all have a Noise method
         public virtual string MakeNoise()
         {
              // A generic animal makes no noise
              return "";
         }
    }
    
    // Now if we create a Cat that inherits from this abstract class, it should implement the Noise method
    public class Cat : Animal
    {
        // This will override the Noise method
        public override string MakeNoise()
        {
             // Make a cat sound
             return "Meow!";
        }
    }

    Overview

    Encapsulation in general is a concrete concept where abstraction is well.. abstract, if that makes any sense. Abstraction focuses more on generalizing things and allowing you to work with them at a more "abstract" level and only implement details when you need to. 

    A great summary from a Stack Overflow user in this discussion can be seen below :

    Encapsulation is hiding the implementation details which may or may not be for generic or specialized behavior(s).

    Abstraction is providing a generalization (say, over a set of behaviors).

    There are tons of great discussions on this topic and I would highly recommend reading through or at least skimming some of them to help give you a more detailed response :

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 3:23 PM

All replies

  • User-484054684 posted

    Wrapping up data and members into single entity is called as encapsulation. Example: You have variables and methods inside a class - this is called as encapsulation.

    Abstraction is hiding the complex or the so called not-needed-to-use information. For example: You want to use some functionality without wanted to know how the things happen internally inside that function. That is nothing but abstraction.

    Friday, July 18, 2014 2:53 PM
  • User281315223 posted

    What they are :

    Abstraction is "the process of identifying common patterns that have systematic variations. Abstract classes provide a way to force an inherited class to impliment an override method, similar to, but not the same as an interface. Unlike an interface, abstract classes are fully functional classes, but you can force one or more methods to be overridden.

    Encapsulation is the ability for an object to hide its data and methods from those who do not need to know, and only expose data and methods that are required. With other words, is the process of protecting the data using properties, so that any attempt to modify the data is done through the proper channels, ideally with an audit log for the bank account class.

    Encapsulation

    Encapsulation is the concept of "information or data hiding" and it focuses primarily on hiding the necessary implementation details and it can also be the concept that handles "grouping" different properties, methods and functions into a single object. You'll see this quite often within C# with the use of classes and structures, which "encapsulates" one or more properties, methods, functions, behaviors into a single object (a class in many cases) and uses this class to function as a single logical unit : 

    public class Widget
    {
        private string WidgetID {get; set;}
        
        public void TurnOn()
        {
             // Do something here
        }
    
        public void TurnOff()
        {
             // Do something here
        }
    }

    The above class can be an example of encapsulation as it wraps a single property and two methods within it and functions as a single object. This can be used to not only to bundle data, but also to restrict access through access modifiers (public, private, internal, etc.) and other approaches.

    Abstraction

    Abstraction is a bit similar to encapsulation in the sense that it also allows you to "hide" things, but it does it in a different way. Where encapsulation above would allow you to hide actual data and properties, abstraction allows you to hide implementation details and generalize what an object or class can do. You'll commonly see abstraction being used in both interfaces and abstract classes as it will essentially provide a blueprint for a class, but it will not have any specific implementation details.

    For instance, you might have an abstract class called an Animal and the animal might make a noise as seen below :

    public abstract class Animal
    {
         // Every animal makes a noise, so they should all have a Noise method
         public virtual string MakeNoise()
         {
              // A generic animal makes no noise
              return "";
         }
    }
    
    // Now if we create a Cat that inherits from this abstract class, it should implement the Noise method
    public class Cat : Animal
    {
        // This will override the Noise method
        public override string MakeNoise()
        {
             // Make a cat sound
             return "Meow!";
        }
    }

    Overview

    Encapsulation in general is a concrete concept where abstraction is well.. abstract, if that makes any sense. Abstraction focuses more on generalizing things and allowing you to work with them at a more "abstract" level and only implement details when you need to. 

    A great summary from a Stack Overflow user in this discussion can be seen below :

    Encapsulation is hiding the implementation details which may or may not be for generic or specialized behavior(s).

    Abstraction is providing a generalization (say, over a set of behaviors).

    There are tons of great discussions on this topic and I would highly recommend reading through or at least skimming some of them to help give you a more detailed response :

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 3:23 PM