Kamis, 03 April 2008

Advanced OOP with Visual Basic.Net - Chapter 3

Objectives

Discuss properties
Implement polymorphism
Implement Inheritance
Implement Method Overriding
Work with Interfaces

Properties

Properties enable us to pass data between a program and a class.

Properties in a class module can be created in two ways:
Creating a Public Variable
Creating a Property procedure

Public and Private Variables

Public variables are created using the Public keyword.
This provides open access to the information in the class.

Public Dim Rate as Integer

Using a public variable does not enable us to create the read-only properties that are often required by a class.
Private variables can be declared as follows:

Private Ac_num As Integer
Private Balance As Integer

Property Routines
Private strName As String
Public Property Name() As String
Set
strName = Value (note : Assignment function:from Set upto Get)
End Set
Get
Name = strName (note: Accessor function : from Get upto End Get)
End Get
End Property



Types of Properties

o Readonly properties

o Writeonly properties

o Default properties


ReadOnly Properties

Declaration may include Get Block without Set Block
Declaration may include a ReadOnly keyword


Public ReadOnly Property Age() As Integer
Get
Age = 3
End Get
End Property


WriteOnly Properties

Declaration may include Set Block without Get Block
Declaration may include a WriteOnly keyword, which ensures that we have Set routine without a Get Block

Public WriteOnly Property Data() As Integer
Set
MintData = Value
End Set
End Property

Default Properties

Declaration includes the Default keyword
VB.net imposes a restriction on default properties – they must have at least one required parameter


Default Public Property Name(ByVal Index As Integer) As String

Polymorphism

Polymorphism enables a single method to exhibit different behaviors when used with different objects.
Method overloading is one form of polymorphism, which allows us to have two or more method declarations with the same name as long as their parameters differ.

Example of Overloading

Public Overloads Function Finder (ByVal Str1 As String) As ArrayList
‘ find data and return result
End Function

Function listed above can be overloaded as:

Public Overloads Function Finder (ByVal Num1 As Integer) As ArrayList
‘ find data and return result
End Function

Overloading Constructors

Public Overloads Sub New()
‘ Initialize object here
End Sub
Public Overloads Sub New(ByVal ID As Integer)
‘use the ID value to initialize the object
End Sub

In this example, we have two implementations of the New method. This approach avoids a conditional check, simplifies code and makes the use of object easier to understand.

Overloading Constructors

Public Overloads Sub New()
‘ Initialize object here
End Sub
Public Overloads Sub New(ByVal ID As Integer)
‘use the ID value to initialize the object
End Sub

In this example, we have two implementations of the New method. This approach avoids a conditional check, simplifies code and makes the use of object easier to understand.

Inheritance

Inheritance is the ability of a class to gain the properties and behavior of an
existing class.
The process by which this is accomplished is called subclassing.
VB.net does not support multiple inheritance.

An Example

Public Class Books
Private BookID,BookCopies As Integer
Private BookName As String
Private BookPrice As Single

Public Property ID() As Integer
Get
Return BookID
End Get
Set
BookID = value
End Set
End Property

Public Property Item() As String
Get
Return BookName
End Get
Set
BookName = Value
End Set
End Property

Public Property Price() As Single
Get
Return BookPrice
End Get
Set
BookPrice = value
End Set
End Property

Public Property Quantity() As Single
Get
Return BookCopies
End Get
Set
BookCopies = value
End Set
End Property

Public Function Amount() As float
Return BookCopies * BookPrice
End Function

End Class

Example of Subclass

Public Class ThrillerBooks
Inherits Books

Private BookDescription As String
Public ReadOnly Property Description() As String
Get
Return BookDescription
End Get
End Property
Public Sub New(ByVal CopyID As String)
Item = CopyID
BookDescription = “Test book description”
End Sub
End Class

Another Subclass

Public Class TragedyBooks
Inherits Books
Private BookDatePurchased As Date
Public Sub New()
Quantity = 1
End Sub
Public Property DatePurchased() As Date
Get
Return BookDatePurchased
End Get
Set

BookDatePurchased = Value
End Set
End Property
End Class

By default, a class allows other classes to be inherited from it. This can be prevented (if required) by the use of the NotInheritable keyword.

Public NotInheritable Class TragedyBooks

Virtual Methods

Virtual method is the last implementation of a method in class hierarchy and is always used in favor of the parent implementation regardless of the data type of the variable being created.
Consider the following example for better understanding:

Dim obj As Parent
obj = New SubClass()
obj.DoTask()


All methods in VB.Net are virtual.

MyBase Keyword

o MyBase keyword allows to explicitly call methods from the immediate parent class.

Public Class SubClassOne
Inherits Parent
Public Overrides Sub DoTask()
System.Console.WriteLine(“Hello from SubClass”)
MyBase.DoTask()
End Sub
End Class

MyClass Keyword

We do not always desire the virtual method behavior of methods in subclasses.

There are times when we want the code in the Parent class to know for certain that it is calling the method implemented from Parent. To accomplish this, we use the MyClass keyword.

An Example

Public Class Parent
Public Sub DoTask()
MyClass. PrintLine()
End Sub
Public Overridable Sub PrintLine()
System.Console.WriteLine(“Parent PrintLine”)
End Sub
End Class


Overriding Constructors

There are special rules that govern the process of overriding the New constructor method.

Public Class SubClass Inherits Parent

Public Sub New()
MyBase.New()
‘other initialization code goes here
End Sub

End Class


MustInherit, MustOverride

To create a class that is to be solely used as a base class for inheritance, the MustInherit keyword is used. No objects of this class can be created.


Public MustInherit Class Books

To create a method that must be overridden by a subclass, the MustOverride keyword is used at the time of declaration of a method.

Public MustOverride Sub CalcPrice()

Abstract Classes

An entire class can be made as an abstract class. This is a class that provides no implementation; all it gives is the interface definition from which a subclass can be derived.

Public MustInherit Class Parent

Public MustOverride Sub DoTask()
Public MustOverride Sub DoOtherTask()
End Class


Interfaces

An interface is declared using the Interface keyword.

Public Interface Inter1
Event Evt()
Sub Met ()
Function Func (ByVal Param As Integer) As Integer
Property Prop() As String
End Interface

Overloads with Interfaces

A method that is either a Sub or a Function can be overloaded in an interface using the Overloads keyword.

Public Interface Inter1

Overloads Sub Met()
Overloads Sub Met( Data As String)
Overloads Function Func(ByVal Param1 As Integer) As Integer
Overloads Function Func(ByVal Param1 As Single) As Integer
End Interface


Implementing Interfaces

We can mark a method in our class as being the implementation of a specific method from the interface:

Class Product
Implements Inter1
Public Sub Met(Data) as Integer Implements Inter1.Met
place code to implement this method
End Sub
End Class

Once this is done, we can create client code that interacts with our objects via this interface.

Tidak ada komentar: