Abstraction Vs Encapsulation

JagpalS

New member
Joined
Sep 17, 2019
Messages
3
Programming Experience
3-5
I very confused in Abstraction Vs Encapsulation, every explains theoretically different, but i am looking something more practical explanation.

As per my understanding if i am creating a method so whatever code i write inside it ,its called encapsulation and if i declare it as private or public that's abstraction.

But in both case i am hiding something from out side world so how both are different.
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
Keeping it basic here, but abstraction is just another way to represent the specific particulates without including implementation details in said scope. Consider encapsulation as internal connection between objects as generally hidden from view outside of the object's definition or defined scope. If you can give an example of what is confusing you, maybe we can give you some examples to help you understand
 

JagpalS

New member
Joined
Sep 17, 2019
Messages
3
Programming Experience
3-5
Lets consider below simple example of class
C#:
    public class A
    {
        public A()
        {
            Console.WriteLine("this is normal ctor of class A");
        }
        public void Method1()
        {
            PrivateMethod();
            Console.WriteLine("this is method of class A");
        }
        private void PrivateMethod()
        {
            Console.WriteLine("this is private method of class A");
        }
    }

where in this code we can say that this is encapsulation and this is abstraction.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,190
Location
Sydney, Australia
Programming Experience
10+
How about you tell us what you think and we can tell you if you're right or why you're wrong?

By the way, this seems a fairly fundamental concept for someone with 5-10 years experience to be troubled with? Is your profile accurate?
 

JagpalS

New member
Joined
Sep 17, 2019
Messages
3
Programming Experience
3-5
@jmcilhinney yes there is small correction in profile but still I understand that it's fundamental concept to trouble with for experienced developer. But I am just trying to map my understanding of definitions of these two terms with the implementation.

Abstraction: Showing necessary.
Encapsulation: Hiding complexity.

So as per my understanding i am only showing method1 not the privatemethod to outside world so in this way i achieved abstraction.

Everything written inside curly brackets is encapsulation because we are hiding the implementation details.
 
Top Bottom