Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Encapsulation Defined

For you new programmers or programmers new to OOP, this is the first of
4 posts that will briefly explain the 4 major principles that make a
language object-oriented: Data Abstraction, Encapsulation, Polymorphism
and Inheritence. All
examples will be in VB.Net, because in my opinion
its easier for a new OOP programmer to read and understand at first.
Certainly don’t think I’m saying you should use one .Net based language
over
another, as they all are based on the CLR/CLS and all end up as the
same assembly language when compiled. Its your preference that
determines what language you use.  Of course, there are other OOP
languages out there, such as Ruby, a pure OOP language, and hybrid
languages such as Python, C++ and Java to mention a few.

What is encapsulation? Well, in a nutshell, encapsulation is the hiding
of data implementation by restricting access to accessors and mutators.
First, lets define accessors and mutators:

Accessor
An accessor is a method that is used to ask an object
about itself. In OOP, these are usually in the form of properties,
which have, under normal conditions, a get method, which is
an accessor method. However, accessor methods are not restricted to
properties and can be any public method that gives information about
the state of the object.

Public Class Person

' We use Private here to hide the implementation of the objects
' fullName, which is used for the internal implementation of Person.
Private fullName As String = "Raymond Lewallen"

' This property acts as an accessor. To the caller, it hides the
' implementation of fullName and where it is set and what is
' setting its value.
It only returns the fullname state of the
' Person object, and nothing more.
From another class, calling
' Person.FullName() will return "Raymond Lewallen".
' There are other things, such as we need to instantiate the
' Person class first, but thats a different discussion.
Public ReadOnly Property FullName() As String Get Return fullName
End Get End Property End Class

Mutator Mutators are public methods that are used to modify the
state of an object, while hiding the implementation of exactly how the
data gets modified. Mutators are commonly another portion of the
property discussed above, except this time its the set method that lets the caller modify the member data behind the scenes.

Public Class Person

' We use Private here to hide the implementation of the objects
' fullName, which is used for the internal implementation of Person.
Private fullName As String = "Raymond Lewallen"

' This property now acts as an accessor and mutator. We still
' have hidden the implementation of fullName.
Public Property FullName() As String Get Return fullName
End Get Set(ByVal value As String)
fullName = value
End Set End Property End Class

Ok, now lets look at a different example that contains an accessor and a mutator:

Public Class Person

Private fullName As String = "Raymond Lewallen" ' Here is another example of an accessor method,
' except this time we use a function.
Public Function GetFullName() As String Return fullName
End Function ' Here is another example of a mutator method,
' except this time we use a subroutine.
Public Sub SetFullName(ByVal newName As String)
fullName = newName
End Sub End Class

So, the use of mutators and accessors provides many advantages. By
hiding the implementation of our Person class, we can make changes to
the Person class without the worry that we are going to break other
code that is using and calling the Person class for information. If we
wanted, we could change the fullName from a String to an array of
single characters (FYI, this is what a string object actually is behind
the scenes) but they callers would never have to know because we would
still return them a single FullName string, but behind the scenes we
are dealing with a character array instead of a string object. Its
transparent to the rest of the program. This type of data protection
and implementation protection is called Encapsulation. Think of accessors and mutators as the pieces that surround the data that forms the class.

Next to come: Abstraction, which is closely tied to encapsulation.

Currently listening to: Renegade Master – Fatboy Slim

This entry was posted in Patterns and Practices. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

One Response to Encapsulation Defined

  1. alu says:

    thanks for your writeup, you explain the terms to a lay mans lang.keep it up

Leave a Reply