Simple way to understand Encapsulation and Abstraction

后端 未结 15 1129
谎友^
谎友^ 2020-11-27 09:47

Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth.

Checked out the below already

Abstraction VS Information Hi

相关标签:
15条回答
  • 2020-11-27 10:40

    Encapsulation is what it sounds like, a way of putting a box around something to protect its contents. Abstraction is extracting the functional properties of something such that you can perform operations using only what you've extracted without knowledge of the inner workings.

    When we say that two substances are liquids we are using "liquid" as an abstraction over the properties of those substances we're choosing to discuss. That abstraction tells us the things we can do with the substances given our previous experience with liquids.

    Abstraction also doesn't really have anything to do with heirarchies. You can have another abstraction like "metals" that extracts properties of substances in a different way.

    Abstractions forget details, so if you're using a particular abstraction you shouldn't ask about properties of the underlying substance that aren't granted by the abstraction. Like if you take milk and water and mix them together, you have a hard time then asking how much milk you have.

    A Functor is an abstraction over something that has some notion of map, that is, you can run a function on its inner contents that transforms the inner bit into anything else. The outer something stays the same kind of thing.

    Where this gets useful is that if you have a function that works on Lists and you realise you're only depending on the map interface, you can instead depend on Functor and then your function can work with streams, promises, maybes, tuples, and anything else that shares that abstraction.

    Functional languages like Haskell have some really great powers of abstraction that make extreme code reuse practical.

    0 讨论(0)
  • 2020-11-27 10:41

    data abstraction: accessing data members and member functions of any class is simply called data abstraction.....

    encapsulation: binding variables and functions or 1 can say data members or member functions all together in a single unit is called as data encapsulation....

    0 讨论(0)
  • 2020-11-27 10:43

    Abstraction is generalised term. i.e. Encapsulation is subset of Abstraction.

    Abstraction is a powerful methodology to manage complex systems. Abstraction is managed by well-defined objects and their hierarchical classification.

    For example a car in itself is a well-defined object, which is composed of several other smaller objects like a gearing system, steering mechanism, engine, which are again have their own subsystems. But for humans car is a one single object, which can be managed by the help of its subsystems, even if their inner details are unknown. Courtesy


    Encapsulation: Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.

    Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

    Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    class Bag{
        book;
        pen;
        ReadBook();
    }
    

    Encapsulation means hiding the internal details of an object, i.e. how an object does something.

    Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

    Encapsulation is a technique used to protect the information in an object from the other object.

    Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.

    So, when you access the property you can validate the data and set it. Courtesy

    0 讨论(0)
  • 2020-11-27 10:43

    Encapsulation: I think this is much to do with how you can bind things into one entity rather than hiding. If you choose to hide something you can.

    Abstraction: Abstraction is much to do with the hiding things and there could be varied levels of abstraction. For example, in functional abstraction we might say that it is important to be able to add items to a list, but the details of how that is accomplished are not of interest and should be hidden. Using data abstraction, we would say that a list is a place where we can store information, but how the list is actually implemented (e.g., as an array or as a series of linked locations) is unimportant and should be hidden.

    Reference

    0 讨论(0)
  • 2020-11-27 10:45

    Encapsulation can be thought of as wrapping paper used to bind data and function together as a single unit which protects it from all kinds of external dirt (I mean external functions).

    Abstraction involves absence of details and the use of a simple interface to control a complex system.

    For example we can light a bulb by the pressing of a button without worrying about the underlying electrical engineering (Abstraction) .

    However u cannot light the bulb in any other way. (Encapsulation)

    0 讨论(0)
  • 2020-11-27 10:47

    Abstraction is Showing necessary info to the user where as Encapsulation hide the unwanted data from the user(Product from the user).

    Encapsulation Implements the Abstraction.

    Abstraction is the process where as Encapsulation actually implements it. For Eg. Adding user logic -> we need to validate the user , creating DB connection and insert the User. So user do not know fist need to call validate function , creating DB connection and then insert the Value in DB. He only call the AddUser function which call the internally all logic with in , this is only Encapsulation (Grouping the feature and hiding the methods).

    0 讨论(0)
提交回复
热议问题