Friday, May 9, 2008

Abstract Classes

Abstract Classes

An abstract class is the one that is not used to create objects. An abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built. Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on it's own, it must be inherited. Like interfaces, abstract classes can specify members that must be implemented in inheriting classes. Unlike interfaces, a class can inherit only one abstract class. Abstract classes can only specify members that should be implemented by all inheriting classes.

Creating Abstract Classes

In Visual Basic .NET we create an abstract class by using the MustInherit keyword. An abstract class like all other classes can implement any number of members. Members of an abstract class can either be Overridable (all the inheriting classes can create their own implementation of the members) or they can have a fixed implementation that will be common to all inheriting members. Abstract classes can also specify abstract members. Like abstract classes, abstract members also provide no details regarding their implementation. Only the member type, access level, required parameters and return type are specified. To declare an abstract member we use the MustOverride keyword. Abstract members should be declared in abstract classes.

Implementing Abstract Class

When a class inherits from an abstract class, it must implement every abstract member defined by the abstract class. Implementation is possible by overriding the member specified in the abstract class. The following code demonstrates the declaration and implementation of an abstract class.

Module Module1

Public MustInherit Class AbstractClass
'declaring an abstract class with MustInherit keyword
Public MustOverride Function Add() As Integer
Public MustOverride Function Mul() As Integer
'declaring two abstract members with MustOverride keyword
End Class

Public Class AbstractOne
Inherits AbstractClass
'implementing the abstract class by inheriting
Dim i As Integer = 20
Dim j As Integer = 30
'declaring two integers

Public Overrides Function Add() As Integer
Return i + j
End Function
'implementing the add method

Public Overrides Function Mul() As Integer
Return i * j
End Function
'implementing the mul method

End Class

Sub Main()
Dim abs As New AbstractOne()
'creating an instance of AbstractOne
WriteLine("Sum is" & " " & abs.Add())
WriteLine("Multiplication is" & " " & abs.Mul())
'displaying output
Read()
End Sub

End Module


The output of above code is the image below.

Wednesday, May 7, 2008

Interfaces

Interfaces

Interfaces allow us to create definitions for component interaction. They also provide another way of implementing polymorphism. Through interfaces, we specify methods that a component must implement without actually specifying how the method is implemented. We just specify the methods in an interface and leave it to the class to implement those methods. Visual Basic .NET does not support multiple inheritance directly but using interfaces we can achieve multiple inheritance. We use the Interface keyword to create an interface and implements keyword to implement the interface. Once you create an interface you need to implement all the methods specified in that interface. The following code demonstrates the use of interface.

Imports System.Console
Module Module1

Sub Main()
Dim OneObj As New One()
Dim TwoObj As New Two()
'creating objects of class One and Two
OneObj.disp()
OneObj.multiply()
TwoObj.disp()
TwoObj.multiply()
'accessing the methods from classes as specified in the interface
End Sub

End Module

Public Interface Test
'creating an Interface named Test
Sub disp()
Function Multiply() As Double
'specifying two methods in an interface
End Interface

Public Class One
Implements Test
'implementing interface in class One

Public i As Double = 12
Public j As Double = 12.17

Sub disp() Implements Test.disp
'implementing the method specified in interface
WriteLine("sum of i+j is" & i + j)
Read()
End Sub

Public Function multiply() As Double Implements Test.Multiply
'implementing the method specified in interface
WriteLine(i * j)
Read()
End Function

End Class

Public Class Two
Implements Test
'implementing the interface in class Two

Public a As Double = 20
Public b As Double = 32.17

Sub disp() Implements Test.disp
WriteLine("Welcome to Interfaces")
Read()
End Sub

Public Function multiply() As Double Implements Test.Multiply
WriteLine(a * b)
Read()
End Function

End Class


Output of above code is the image below.

Monday, May 5, 2008

Polymorphism

Polymorphism

Polymorphism is one of the crucial features of OOP. It means "one name, multiple forms". It is also called as Overloading which means the use of same thing for different purposes. Using Polymorphism we can create as many functions we want with one function name but with different argument list. The function performs different operations based on the argument list in the function call. The exact function to be invoked will be determined by checking the type and number of arguments in the function.

The following code demonstrates the implementation of Polymorphism.

Module Module1

Sub Main()
Dim two As New One()
WriteLine(two.add(10))
'calls the function with one argument
WriteLine(two.add(10, 20))
'calls the function with two arguments
WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Read()
End Sub

End Module

Public Class One
Public i, j, k As Integer

Public Function add(ByVal i As Integer) As Integer
'function with one argument
Return i
End Function

Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
'function with two arguments
Return i + j
End Function

Public Function add(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer) As Integer
'function with three arguments
Return i + j + k
End Function

End Class


Output of the above code is shown in the image below.

Saturday, May 3, 2008

Inheritance

Inheritance

A key feature of OOP is reusability. It's always time saving and useful if we can reuse something that already exists rather than trying to create the same thing again and again. Reusing the class that is tested, debugged and used many times can save us time and effort of developing and testing it again. Once a class has been written and tested, it can be used by other programs to suit the program's requirement. This is done by creating a new class from an existing class. The process of deriving a new class from an existing class is called Inheritance. The old class is called the base class and the new class is called derived class. The derived class inherits some or everything of the base class. In Visual Basic we use the Inherits keyword to inherit one class from other. The general form of deriving a new class from an existing class looks as follows:

Public Class One
---
---
End Class

Public Class Two
Inherits One
---
---
End Class


Using Inheritance we can use the variables, methods, properties, etc, from the base class and add more functionality to it in the derived class. The following code demonstrates the process of Inheritance in Visual Basic.

Imports System.Console
Module Module1

Sub Main()
Dim ss As New Two()
WriteLine(ss.sum())
Read()
End Sub

End Module

Public Class One
'base class
Public i As Integer = 10
Public j As Integer = 20

Public Function add() As Integer
Return i + j
End Function

End Class

Public Class Two
Inherits One
'derived class. class two inherited from class one
Public k As Integer = 100

Public Function sum() As Integer
'using the variables, function from base class and adding more functionality
Return i + j + k
End Function

End Class


Output of above code is sum of i, j, k as shown in the image below.

Friday, May 2, 2008

Constructors in VB.NET

Constructors in VB.NET:::

A constructor is a special member function whose task is to initialize the objects of it's class. This is the first method that is run when an instance of a type is created. A constructor is invoked whenever an object of it's associated class is created. If a class contains a constructor, then an object created by that class will be initialized automatically. We pass data to the constructor by enclosing it in the parentheses following the class name when creating an object. Constructors can never return a value, and can be overridden to provide custom intitialization functionality. In Visual Basic we create constructors by adding a Sub procedure named New to a class. The following code demonstrates the use of constructors in Visual Basic.

Module Module1

Sub Main()

Dim con As New Constructor(10)
WriteLine(con.display())
'storing a value in the constructor by passing a value(10) and calling it with the
'display method
Read()

End Sub

End Module

Public Class Constructor
Public x As Integer

Public Sub New(ByVal value As Integer)
'constructor
x = value
'storing the value of x in constructor
End Sub

Public Function display() As Integer
Return x
'returning the stored value
End Function

End Class


Destructors

A destructor, also know as finalizer, is the last method run by a class. Within a destructor we can place code to clean up the object after it is used, which might include decrementing counters or releasing resources. We use Finalize method in Visual Basic for this and the Finalize method is called automatically when the .NET runtime determines that the object is no longer required. When working with destructors we need to use the overrides keyword with Finalize method as we will override the Finalize method built into the Object class. We normally use Finalize method to deallocate resources and inform other objects that the current object is going to be destroyed. Because of the nondeterministic nature of garbage collection, it is very hard to determine when a class's destructor will be called. The following code demonstrates the use of Finalize method.

Module Module1

Sub Main()
Dim obj As New Destructor()
End Sub

End Module

Public Class Destructor

Protected Overrides Sub Finalize()
Write("hello")
Read()
End Sub

End Class


When you run the above code, the word and object, obj of class, destructor is created and "Hello" is displayed. When you close the DOS window, obj is destroyed.