What is difference between Abstraction and Encapsulation? Please give some example also because it is always confusing my.
Raghvendra Singh
Consider class as a encapsulation. where you will add all the components which is required to perform certain operation. And consider a property of a class as a abstraction which can be used to do certain logic or data.
Encapsulation is the process of keeping state and behavior of the object in a single unit and exposing public methods to act upon the state means hiding internal data of the object from the outside world. Abstraction is a process of identifying the relevant qualities and behvaiors an object should possess. Abstraction is a process. It is the act of identifying the relevant qualities and behaviors an object should possess. Encapsulation is the mechanism by which the abstraction is implemented.
AbstractinsExample: I have class Animal and i am making a sub class of the class Animal called Tiger.Then i will say that its colour is yellow and running is very fast.Also i can make the class Hippo but it's colour is black and running is very slow.This is an example of Abstraction.Encapsulation:Example: Just think of a capsule that the doctor gives us.We just have to take the capsule to get better. We don't have to worry about what medicine is inside the capsule or how it will work on our body.What it means that we give access to our class via proper methods and properties. The user does not have to worry how this methods and properties work.
Okay!!!There are two OO principles that lead the way for a good S/W design1. Low coupling : Dependency of one component on another component should be as minimum as possible and all components should be as independent as possible. It is highly desirable for a component NOT to reveal its private information to other components. This leads to designs where only interfaces (in C++ abstract classes) are exposed without any data members. Also the arguments to behaviors that an interface exposes to other component should be as primitive as possible so that other components have a very easy time in communicating with it. This leads to DATA ABSTRACTION. So data abstraction is achieved by the way of interfaces and abstract classes.What is the use for Data Abstraction? Well let us say a method is returning an ArrayList class directly and later we have diced to return a LinkedList object rather than ArrayList, we can't do that easily as all the client programs that are calling the method need to change. Think about the method returning a List interface object rather than an ArrayList. Now it is easy for us to return a LinkedList becasue that is also a List. This means we ABSTRACTED the actual data portion i.e. "Array" and "Linked" and we are just returning "List".2. High cohesion: A component should be self sufficient enough as much as possible. For all its needs it should look into itself. That means all the data items and operations that use those data items should be close to each other in the same component. This leads to DATA-BEHAVIOR ENCAPSULATION. So in a component encapsulation is achieved by the way of making data members private and expose (bare minimum) public methods to other components so that outside components doesn't need to deal with the organization of dataWhat is the use for High Cohesion? It is similar to having a electric switch near to your bed so that as soon as you want to sleep after reading a book in the night, you don't have to get up. Imagine a switch for you room 3 floors above. Now when we discuss this technically First of all ENCAPSULATION protects DATA ABSTRACTION. Secondly it provides a flexibility of changing internal data without effecting other components.Hope these two concepts are clear now
Abstraction meas hiding the complexcity of program i.e. it is the manner how write a program in simplest way.
Encapsulation is, wrapping a data and method into a single unit(Class) i.e. hiding all internal details of an objects to outside.
Data Abstraction: Data Abstraction is nothing but hide the client details,like bike is hide the type of eingine and type of wheels etc.It is provide safty and protection etc.
An abstract data type is a user-defined data type that satisfies the following two conditions:The representation of, and operations on, objects of the type are defined in a single syntactic unitThe representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition
Go through this link .u get examples for all Oops .http://www.dotnetspider.com/resources/18616-OOPS-Concepts-Net-Shake-your.aspx
Abstraction means to show essential details to the client.
Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT. When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed that’s it!! This is abstraction; show only the details which matter to the user.
Encapsulation is hiding non essential details to the client.
The monitor of your pc is wrapped with plastic body and switch is provided for on and off isn’t it ? What will happen if they will give you the monitor without plastic body? Don’t you think so it will be a dangerous enough? If your bike comes with the parts and guide book and let us assume you know how to assemble, do you want to assemble it every day so that you neighbours will come to know the technology? This is encapsulation; hiding the details which are not required by the user gives you safety and protection from overenthusiastic clients.