OOPS Concepts – Data Abstraction and Encapsulation


What is Data Abstraction?

Abstraction is the ability to generalize an object as a data type that has a specific set of characteristics and is able to perform a set of actions.

For example, you can create an abstraction of a Car with characteristics, such as color, height, and weight, and actions such as run. The characteristics are called properties, and the actions are called methods.

What is Data Encapsulation?

The wrapping up of data and functions into a single unit or class is known as Data Encapsulation.

Consider the following example :

''' <summary>
''' Car Class for Dot Net Programming.
''' </summary>
''' <remarks></remarks>
Public Class Car

    Private CarColor As System.Drawing.Color
    Private CarHeight As Double
    Private CarWeight As Double
    ''' <summary>
    ''' Car Property Color
    ''' </summary>
    Public Property Color As System.Drawing.Color
        Get
            Return Me.CarColor
        End Get
        Set(value As System.Drawing.Color)
            Me.CarColor = value
        End Set
    End Property
    ''' <summary>
    ''' Car Property Height
    ''' </summary>
    Public Property Height As Double
        Get
            Return Me.CarHeight
        End Get
        Set(value As Double)
            Me.CarHeight = value
        End Set
    End Property
    ''' <summary>
    ''' Car Property Weight
    ''' </summary>
    Public Property Weight As Double
        Get
            Return Me.CarWeight
        End Get
        Set(value As Double)
            Me.CarWeight = value
        End Set
    End Property
    ''' <summary>
    ''' Action Of Car
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Run()
         Console.WriteLine("Car is running")
    End Sub

End Class
Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s