Imgane5h
Imgane5h

Reputation: 324

Understanding Encapsulation and Abstraction in OOP

I am reading few fundamental things of OOP. I am confused about encapsulation and abstraction. As per my understanding, Abstraction is a way of exposing only few things and hiding few. Encapsulation helps us to encapsulate the state of an object so that it is not accessible to the outside world and it can be accessed through internal methods. Hence they both are correlated. But i have read many times that encapsulation is achieved by using getter and setter while abstraction is achieved using abstract classes and interfaces. Now consider following code (C#)

class A
    {
        private int a;
        public void print()
        {
            Console.WriteLine("Value of a = {0}", a);
        }
    }
    class B
    {
        static void Main(string[] args)
        {
            A a = new A();
            a.print();                
        }
    }

In above code i have not used interfaces nor abstract class but still as per my understanding i have achieved abstraction here by not letting B class access variable a directly. By implementing encapsulation i made sure it is only accessible by using internal methods of A. So can you please help me clear the confusion?

Upvotes: 1

Views: 1606

Answers (2)

Snesh
Snesh

Reputation: 555

You achieved abstraction in your example.

"Abstraction" is oversued term and its exact meaning depends on context too and that's why sometime it confuses us. By definition "Abstraction captures only those details about an object that are relevant to the current perspective."

In your case, (User) Class B is asking object "a" (type of class A) to print something without going in detail of how class "A" has implemented it. So only Method Name is relevant to user, not detail.

I was having many doubts around Abstration too but this wikipedia page on Abstraction helped me a lot.

Abstraction is used in many Hardware and software concepts too like HAL, Datbase, multiple languages etc.

As a one of OOPS four pillars, It is "a facility to define objects that represent abstract "actors"(means class) that can perform work (as methods), report on and change their state(events), and "communicate" with other objects in the system."

On other hand, encapsulation is (as given on Wikipedia):

The term encapsulation refers to the hiding of state details, but extending the concept of data type and standardizing the way that different data types interact (is the beginning of abstraction).

But if you take "Abstration" in deeper sense and generalize it (again from Wikipedia page):

When abstraction proceeds into the operations defined, enabling objects of different types to be substituted, it is called polymorphism. When it proceeds in the opposite direction, inside the types or classes, structuring them to simplify a complex set of relationships, it is called delegation or inheritance.

Upvotes: 3

Kumar Manish
Kumar Manish

Reputation: 3772

Encapsulation means Hide the complexitity , Abstraction means expose the essential things.

Example :

Car works with Engine , carburetor and other things , which make it movable ,(This is called Encapsulation ) but User only aware with streeing , wheele, brake , door and accelator which need to know for drive the car (This is called Abstraction)

Upvotes: 2

Related Questions