encapsulation vs abstraction real world example

前端 未结 17 2140
闹比i
闹比i 2020-12-12 10:59

For an example of encapsulation i can think of the interaction between a user and a mobile phone. The user does not need to know the internal working of the mobile phone to

相关标签:
17条回答
  • 2020-12-12 11:39

    The wording of your question is odd - Abstraction vs Encapsulation? It should be - someone explain abstraction and encapsulation...

    Abstraction is understanding the essence of the thing.

    A real world example is abstract art. The artists of this style try to capture/paint the essence of the thing that still allows it to be the thing. This brown smear of 4 lines captures the essence of what a bull is.

    Encapsulation is black boxing.

    A cell phone is a great example. I have no idea how the cell phone connects to a satellite, tower, or another phone. I have no idea how the damn thing understands my key presses or how it takes and sends pictures to an email address or another phone number. I have no idea about the intricate details of most of how a modern smart phone works. But, I can use it! The phones have standard interfaces (yes - both literal and software design) that allows someone who understand the basics of one to use almost all of them.

    How are the two related?

    Both abstraction and encapsulation are underlying foundations of object oriented thought and design. So, in our cell phone example. The notion of a smart phone is an abstraction, within which certain features and services are encapsulated. The iPhone and Galaxy are further abstractions of the higher level abstraction. Your physical iPhone or Galaxy are concrete examples of multiple layers of abstractions which contain encapsulated features and services.

    0 讨论(0)
  • 2020-12-12 11:39

    I guess an egg shell can be consider the encapsulation and the contents the abstraction. The shell protects the information. You cant have the contents of an egg without the shell.,,LOL

    0 讨论(0)
  • 2020-12-12 11:39

    Just think of Abstraction as hiding the keypad and display screen details, Encapsulation as hiding the internal circuitry that binds them.

    0 讨论(0)
  • 2020-12-12 11:39

    Abstraction

    We use many abstractions in our day-to-day lives.Consider a car.Most of us have an abstract view of how a car works.We know how to interact with it to get it to do what we want it to do: we put in gas, turn a key, press some pedals, and so on. But we don't necessarily understand what is going on inside the car to make it move and we don't need to. Millions of us use cars everyday without understanding the details of how they work.Abstraction helps us get to school or work!

    A program can be designed as a set of interacting abstractions. In Java, these abstractions are captured in classes. The creator of a class obviusly has to know its interface, just as the driver of a car can use the vehicle without knowing how the engine works.

    Encapsulation

    Consider a Banking system.Banking system have properties like account no,account type,balance ..etc. If someone is trying to change the balance of the account,attempt can be successful if there is no encapsulation. Therefore encapsulation allows class to have complete control over their properties.

    0 讨论(0)
  • 2020-12-12 11:40

    Everything has many properties and behaviours so take whatever object you want TV, Mobile, Car, Human or anything.

    Abstraction:

    1. Process of picking the essence of an object you really need
    2. In other words, pick the properties you need from the object Example:
      a. TV - Sound, Visuals, Power Input, Channels Input.
      b. Mobile - Button/Touch screen, power button, volume button, sim port.
      c. Car - Steering, Break, Clutch, Accelerator, Key Hole.
      d. Human - Voice, Body, Eye Sight, Hearing, Emotions.

    Encapsulation:

    1. Process of hiding the details of an object you don't need
    2. In other words, hide the properties and operations you don't need from the object but are required for the object to work properly Example:
      a. TV - Internal and connections of Speaker, Display, Power distribution b/w components, Channel mechanism.
      b. Mobile - How the input is parsed and processed, How pressing a button on/off or changes volumes, how sim will connect to service providers.
      c. Car - How turning steering turns the car, How break slow or stops the car, How clutch works, How accelerator increases speed, How key hole switch on/of the car.
      d. Human - How voice is produced, What's inside the body, How eye sight works, How hearing works, How emotions generate and effect us.

    ABSTRACT everything you need and ENCAPSULATE everything you don't need ;)

    0 讨论(0)
  • 2020-12-12 11:43

    If you have seen important TV machine, TV connections and TV color tube is hidden inside the TV case which is not been exposed for viewers like us and exposed only neccessary things of a TV like TV Channel keys, TV volume keys, ON/OFF switch, Cable Switch and TV remote control for viewers to use it.This means TV machine, TV connections and TV color tube is an unwanted data and not needed for viewers to see is been hidden from outside the world

    So encapsulation means hiding the important features of a class which is not been needed to be exposed outside of a class and exposing only necessary things of a class. Here hidden part of a class acts like Encapsulation and exposed part of a class acts like Abstraction.

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