Home Getting Started With Visual Basic .NET Object Oriented Programming in VB.NET
Object Oriented Programming in VB.NET PDF Print E-mail
User Rating: / 67
PoorBest 

Object Oriented Programming in VB.NET

Information hiding is the concept of exposeing only essential information and hiding the inner logic. It is used mostly in  hiding of design decisions that are most likely to change. Information hiding also manifests in Encapsulation and Polymorphism. The term Encapsulation is used interchangeabley with information hiding Encapsulation results in encapsulating the data and the processes into a module or other construct which presents an interface.

Polymorphism is where multiple procedures of the same name perform different tasks, Polymorphism is the process of using an function or a procedure in different ways for different set of inputs given. Polymorphism in VB.NET is through the definition and implementation of a common interface. You can use polymorphism to hide logic from the programmer

Inheritance helps you create new objects from existing objects for example you can Inherit one form from another form. One common reason to use inheritance is to create specializations of existing classes or objects.  . For example, a "Bank Account" class might have data for an "account number", "owner", and "balance". An "Interest Bearing Account" class might inherit "Bank Account" and then add data for "interest rate" and "interest accrued" along with behavior for calculating interest earned.

Programming object mimics the behavior of a real-world object For ex when a Button on a form perform a task when clicked some objects are visible to the user while others are not Objects are created from classes Multiple objects can be created from the same class. For Example The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Snoopy is one particular dog, with particular versions of the characteristics. A Dog has fur; Snoopy has white fur.

A Class Defines the abstract characteristics of a object, including the thing's characteristics (its attributes, fields or properties) and the object's behaviors (the things it can do, or methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of the class should make sense in context. Also, the code for a class should be relatively self-contained (generally using encapsulation). Collectively, the properties and methods defined by a class are called members.

Example for OOP in VB.NET


Namespace std
 
    Public Interface IAnimal
        ReadOnly Property Name() As String
        Function Talk() As String
    End Interface
 
    Public Class Cat
        Implements IAnimal
 
        Private mName As String
 
        Sub New(ByVal name As String)
            mName = name
        End Sub
 
        Public ReadOnly Property Name() As String Implements IAnimal.Name
            Get
                Return mName
            End Get
        End Property
 
        Public Function Talk() As String Implements IAnimal.Talk
            Return "Meow!"
        End Function
    End Class
 
    Public Class Dog
        Implements IAnimal
 
        Private mName As String
 
        Sub New(ByVal name As String)
            mName = name
        End Sub
 
        Public ReadOnly Property Name() As String Implements IAnimal.Name
            Get
                Return mName
            End Get
        End Property
 
        Public Function Talk() As String Implements IAnimal.Talk
            Return "Woof! Woof!"
        End Function
    End Class
 
    Public Module TestAnimals
 
        ' Prints the following:
        '
        ' Healthcliff: Meow!
        ' Garfield: Meow!
        ' Snoopy: Arf! Arf!
        Public Sub Main()
            Dim animals(3) As IAnimal
            animals(0) = New Cat("Healthcliff")
            animals(1) = New Cat("Garfield")
            animals(2) = New Dog("Snoopy")
 
            For Each a As IAnimal In animals
                Console.Out.WriteLine("{0}: {1}", a.Name, a.Talk)
            Next a
 
        End Sub
    End Module
 
End Namespace

 
 

Advertisiment