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
Class
es.
Also consider this (example in C# since you did not provide a language):
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.