encapsulation vs abstraction real world example

前端 未结 17 2142
闹比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:57

    Abstration which hide internal detail to outside world for example you create a class(like calculator one of the class) but for end use you provide object of class ,With the help of object they will play and perform operation ,He does't aware what type of mechanism use internally .Object of class in abstract form .

    Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.For example class calculator which contain private methods getAdd,getMultiply .

    Mybe above answer will help you to understand the concept .

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

    Abstraction Means We focus on the essential qualities of some thing rather than one specific example and we automatically discard what is unimportant or irrelevant.

    Example We are writing a bank account class,essential qualities of bank account are Opening date, Account title,Account number,Balance etc...

    Encapsulation Means the idea of capsulation or surrounding some thing not just to keep the content together but also to protect and restrict form accessing out side.Along with secrecy It's about reducing dependencies between different parts of the application.

    Example In our Bank account class Someone accessing the attribute of Balance and trying to change it ,Attempt can be successful if there is no encapsulation.

    0 讨论(0)
  • 2020-12-12 12:01

    Encapsulation is a way to achieve "information hiding" so, following your example, you don't "need to know the internal working of the mobile phone to operate" with it. You have an interface to use the device behaviour without knowing implementation details.

    Abstraction on the other side, can be explained as the capability to use the same interface for different objects. Different implementations of the same interface can exist. Details are hidden by encapsulation.

    0 讨论(0)
  • 2020-12-12 12:03

    I feel like encapsulation may make more sense to discuss when you see HOW NOT TO DO in programming. For example, consider a Car class as below.

     class Car{
       public float speed =0;
       public boolean isReverse = false;
       public boolean isStarted = false;
     }
    

    The client code may use above car class as below.

    class Main{
      public static void main(args String[]){
       Car car = new Car();
       // No need to start??
       car.speed = 100; // Turbo mode directly to 100
       car.speed = 0; // Turbo break
      }
    }
    

    See more at: http://brevitaz.com/encapsulation-example-benefits-java/

    This is uncontrolled access to car speed and other variables. By encapsulation, Car class can have complete control over how the data variables within car class can be modified.

    Any concrete entity that has some behavior is example of Encapsulation. The behavior is provided by wrapping up something and hiding something from client.In case of mobile, it is signals, chips, circuits, battery and so on.

    For abstraction of the same example - normal user may say I am ok with anything using which I can make calls and receive calls. This abstraction can be substituted by any concrete mobile. Check out Abstraction examples.

    0 讨论(0)
  • 2020-12-12 12:04

    In General words,Abstraction is Just Hiding the complex things behind a particular Procedure to make the procedure look simple.
    Example:Monitor ON/OFF::--The user doesn't need to know much about all the chips functioning that happens when Monitor is switched ON or OFF..All he needs to know is On Function ON-Monitor is On and on function OFF-Monitor is off...

    Or Better Look for a car--Everyone Knows that There's a special Gear machine Which changes the gear,nobody bother to know what all functionality undergoes for a gear to change..So,That's abstraction(avoiding unwanted implementations to prevent Complexity).

    So,If a developer provides a good abstraction, users won't be tempted to peek at the object's internal mechanisms.

    Abstraction is achieved by making class abstract having one or more methods abstract. Which is nothing but essential characteristic which should be implemented by the class extending it. e.g. when you inventing/designing a car you define a characteristics like car should have 4 doors, break, steering wheel etc… so anyone uses this design should include this characteristics. Implementation is not the head each of abstraction. It will just define characteristics which should be included.

    Encapsulation is restricting a user to follow a particular procedure to access control of a particular process.It Just provides safety and ensures system robustness.

    Example:We can consider The HR in a company as a person that works on the principle of Encapsulation.i.e. we cannot talk to other departments directly we need to communicate through them through HR.This ensures security and better maintenance of company's records.

    Together we can take example of a UNDER CONSTRUCTION BUILDING..where we can say that things like 'no. of managers' required,Types of Materials,No of workers etc as abstraction as they need to there in every Building Construction.

    But,at the same time,Inclusion of every such field into a CONTRACTOR which acts as a mediator between the workers and the Building-Investor can be looked upon as Encapsulation. As,It hides all the above properties into one Entity.

    Hence If you would have understood till now you can say that abstraction is just a subset of ENCAPSULATION.i.e.Every entity that performs abstraction is encapsulated internally but every thing that shows encapsulation need not be abstraction always.

    e.g. .ToString() Method defined in almost every class is implementation of Abstraction because We don't the functionaltiy Within,all we care is that it changes almost everything to string.And as it assembles a s a unit,it is encapsulated too..But,The private members that we hide and access through Properties is an example of encapsulation only as it is done basically keeping data security in mindd..!!

    Hope This answers your Question..!!

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