Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
can we differncitate class and encapsulation
Posted

Encapsulation is essentially "data hiding".

A class is a construct that can be instantiated to create objects in memory. The class will use encapsulation (e.g. public and private methods) to hide attributes that you don't want to allow public access to. Finally an instance of the class will be created. This object will show and hide members based on the encapsulation set in the class.
 
Share this answer
 
Comments
Mehdi Gholam 21-Oct-11 13:08pm    
my 5!
Abhinav S 21-Oct-11 13:21pm    
Thank you.
Sander Rossel 21-Oct-11 13:18pm    
Seems everyone answered at the same time :p
My 5 for the answer.
Abhinav S 21-Oct-11 13:21pm    
Thank you.
No. This is the same as to ask: "What's the difference between Moon and NASA?".

Sorry, but it you don't see that the questions like "What's the difference between {0} and {1}" are incorrect (hope my example above helps you to realize that), I don't want to go into any further OOP-related detail. You can easily find tons of literature about OOP. Of you goal is to learn just about what is "class" and "encapsulation", this is just a waste of time.

—SA
 
Share this answer
 
Comments
Sander Rossel 21-Oct-11 13:21pm    
For some reason people seem to mix these two up. I've seen this question many times before. I must say I didn't quite catch Encapsulation either about a year ago. Luckily I am now fully aware, but Encapsulation can still be tricky at times. As such I decided to give a moderatly detailed answer to this one :)
Sergey Alexandrovich Kryukov 21-Oct-11 13:33pm    
No, people cannot "mix up" those things. Words cannot be combined in arbitrary manner. Those people don't actually mix up notions, they do not understand the notion of "notion", function of words in the language or something like that. Gibberish is gibberish, it has nothing to do with understanding of technology like OOP. Yes, OOP understanding and explanation is considerable problem, but in case of this question this is irrelevant.

--SA
Sander Rossel 21-Oct-11 13:37pm    
Yeah, I do agree with you. But it is noticable that this particular question is asked rather often ;)
And why can people downvote good solutions without having to comment? :(
Uday P.Singh 21-Oct-11 13:49pm    
don't you think votes of 3 or less must require a comment in QA also?
Abhinav S 21-Oct-11 13:22pm    
Yes its actually hard to compare the two. My 5.
Google is your friend, if you are looking stuffs on web.

Now as far as your question is concerned refer this article, it will clear all your doubts:

Introduction to Object Oriented Programming Concepts (OOP) and More[^]

hope it helps :)
 
Share this answer
 
Comments
Sander Rossel 21-Oct-11 13:41pm    
Very nice article, my 5 for the reference.
Uday P.Singh 21-Oct-11 13:49pm    
thanks :)
They are two completely different things.
A Class[^] is a blueprint of a custom type, which can have Methods, Properties, Fields, etc. A Class can be used to create Object by instantiating them and assigning them to variables.
Encapsulation [^] is the level of access an Object provides to the rest of your application. For example a Method can be Private, which means it is only accessible from within its own Class. A Method can be Public, which means any other Object holding a a reference to an instance of the particular Class can call that Method.
I have written an article[^] that, for a part, discusses the benefits of proper Encapsulation of your Classes.
Also consider this (example in C# since you did not provide a language):
C#
public class Test
{

   private int _counter;

   public void IncrementCounter()
   {
      _count += 1;
   }

}

In that example the Class is Test. It Encapsulates an int called _counter. This _counter cannot be directly accessed by other parts of your application. The Method IncrementCounter on the opposite CAN be called from anywhere. So access to _counter is limited to calling the IncrementCounter Method. _counter is enclosed by the Test Class and protected from the exterior environment.
Hope that clears up a thing or two.
 
Share this answer
 
v3
Comments
Abhinav S 21-Oct-11 13:22pm    
Exactly. 5.
Sander Rossel 21-Oct-11 13:24pm    
Thanks :)
Sergey Alexandrovich Kryukov 21-Oct-11 13:46pm    
Sorry, really sorry. This is me who voted "1". Look what are you writing.

"A Class is an Object of a specific type" is a big confusion. Here, two different types are involved, and you mix them up. Consider you have class Cl. It is not an object of class Cl, such object is Cl instance = new Cl(), for example. Nevertheless, Cl is also and object, but different; a class itself considered as object is meta-class which is partially exposed as an object when you take System.Type ClType = typeof(Cl). If you don't speak about Reflection, etc., at least don't call Cl an object, consider only instances of Cl, objects of a type Cl.

Now, encapsulation is not access level; this is just one of the aspects, not the most important. Read Wikipedia article on encapsulation to see what do I mean. I'm afraid your challenge in getting what encapsulation is is not yet complete. If you want, we can discuss it.

Your article seems interesting, I want to read it. I hope you can improve this post; I will gladly re-vote if you put it right. Is such cases just a link to some good articles usually works better. Writing your own explanation in a quick answer is not easy.
--SA
Sander Rossel 21-Oct-11 14:00pm    
Wow, you are strict... I fixed the Class/Object thing. You were right about that (as always).
I am not sure I am getting what you said about Encapsulation though. Keywords to control Encapsulation are actually called access modifiers, such as public, private, protected and friend. By putting these keywords in front of any Class, Property, Method or Field you can limit access to those Object. For the benefits, like making sure an Object does not hold invalid data, I reference to MSDN and my article. So please elaborate on that.
Uday P.Singh 21-Oct-11 13:50pm    
My 5 for your efforts, yes! I think SA is right here on the first point, may be too harsh on second point though.

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