Click here to Skip to main content
14,739,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After read so many article on this I got confuse. To implement encapsulation (or data hiding) concept we are using access specifier and also for prove Abstraction we are using access specifier. kindly someone explain me how i can implement on code.

What I have tried:

I tried with bellow links :

Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation.
Encapsulate hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.
Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.
Abstraction is used to hiding something too but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.
Implementation Difference Between Encapsulation and Abstraction
1. Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.
2. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.
3. Example of Encapsulation
    public int Marks 
      get { return marks; } 
      set { marks = value;} 
4. Example of Abstraction
   abstract class Abstraction { public abstract void Abstractionfun(); }
   public class AbstractionImpl: Abstraction 
       public void Abstractionfun() { //Implementation } 
Encapsulation is about hiding the implementation: a collection class which hides the actual collection from the "outside world" and provides it's own methods to access the collection content for example. Think of a shopping list, or a music play list: you don;t care how the application stores it as long as you can say "I bought this now", or "skip to next track" everything is fine. Encapsulation "puts things in a box" and provides you will a small hole through which you can interact with them - you can't "put your hand in" and start grabbing things! :laugh:

Abstraction is different - it's about generalizing rather than hiding. You use abstraction to "group" related items together by ignoring details, and leaving them up to the individual implementation. For example, think about a Car - it's an abstract quantity, which has common properties and methods regardless of the individual "concrete" example you can drive. It can have a "number of wheels" property, a "Drive" method. But it doesn't need to know - or even care - whether the engine is petrol, diesel, electric, hybrid, elastic bands, or nonexistent. Those are all implementation details which are the concern of implementation classes, not the abstract base.
So the abstract Car object can't be driven, but all Ford, Mercedes, Kia, ... examples of a Car can - without you having to be re-trained to drive them.

Have a look at google - there are some good detailed explanations of both out there that go way beyond what we can put in a little text box like this! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900