encapsulation

davegreen

New member
Joined
Jun 16, 2010
Messages
1
Programming Experience
Beginner
Hi, I have searched and searched for a code example which shows some basic programming that does not use encapsulation and then the same code but changed to represent encapsulation...

All i can find is examples of encapsulated code

Could someone please post me some simple code snippets

-snippet of some simple code that doesnt use encapsulation

and

-snippet of that same code modified to represent encapsulation

any help would be greatley appriciated
 
I think you're a little confused as to what encapsulation is. Encapsulation should be viewed as a black box. As a developer working with the object you know what methods and properties are exposed from the object without needing to know the inner workings.

Here's 2 classes that expose the same interface (properties in this case) but have different implementations:

VB.NET:
Public Class Person
    Property FirstName As String
    Property LastName As String
    Private _FullName As String
    Public ReadOnly Property FullName() As String
        Get
            Return FirstName & " " & LastName
        End Get
    End Property
End Class

VB.NET:
Public Class Person
    Property FirstName As String

    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
            _FullName = FirstName & " " & LastName
        End Set
    End Property

    Private _FullName As String
    ReadOnly Property FullName() As String
        Get
            Return _FullName
        End Get
    End Property
End Class
 
Hi, I have searched and searched for a code example which shows some basic programming that does not use encapsulation and then the same code but changed to represent encapsulation...

as per the wiki article, it can either refer to the notion of bundling data in with methods that operate on that data (the concept of a typical class)

or it can be this black box notion whereby you only see the external interface, and all the functionality and other data stored is done so internally
consider a headache pill or 3 stage slow release dishwasher tablet. you read the outside of the packet, you know what it is, and how to make it do its job, but you have no idea what it contains or how those things work toegether to enable it to do its job

I can't really give any examples without commiting a serious amount of time to writing them. Perhaps you can more accurately explain what part of the definition youre struggling with.
 
ok, as exaplined, encapsulation is where you dont see the workings but what you do see is the method (property, function ect) here would be a good exmaple:
 

Attachments

  • simple example.doc
    60.5 KB · Views: 19
Back
Top