Professional Documents
Culture Documents
VBDotNetUnit3 Notes
VBDotNetUnit3 Notes
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 general form of deriving a new class from an existing class looks as follows:
<access-specifier> Class <base_class>
...
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.
Types of Inheritance
Single Inheritance - Deriving a new class from single base class
Multilevel Inheritance - Deriving a class from another derived class
Hierarchical Inheritance - Deriving more than one class from same base
Multiple Inheritance - Deriving a class from more than one base class.
Not supported directly but in a crude way VB.Net supports multiple
inheritance using interface concept
Polymorphism
The ability to take more than one form is known as polymorphism. VB.Net
implements dynamic polymorphism using Virtual Method.
Sub Main()
Dim r As shape
r = New Rectangle()
r.Read(10, 20)
r.Area()
Console.ReadKey()
End Sub
End Module
Abstract Class
Abstract Classes are classes that cannot be instantiated, and are either
partially implemented or not at all implemented. One key difference between
abstract classes and interfaces is that a class may implement any number of
interfaces, but may inherit from only one abstract class.
The Abstract Classes are declared using the keyword MustInherit. The
MustInherit keyword specifies that a class cannot be instantiated and can be
used only as a base class. An abstract class can contain abstract and concrete
methods. When a class is inherited from an abstract class, it must implement
every abstract method defined by the abstract class or must be defined
abstract itself.
Abstract Methods
Abstract methods are overridable methods that are declared with the
MustOverride keyword and provide no implementation. A class that inherits
from a class with abstract methods must provide an implementation for the
abstract methods or must be abstract itself.
Interface i2
Sub m2()
End Interface
Class sample
Implements i1, i2
Sub Main()
Dim s As New sample()
s.m1()
s.m2()
End Sub
End Module
Interface Inheritance
An interface can inherit from one or more base interfaces. In the following
example, the interface MyInterface inherits from two base interfaces, Base1
and Base2:
interface MyInterface : Implements Base1, Base2
[Members]
End Interface
Example – Interface Inheritance
Module Module1
Interface i1
Sub m1()
End Interface
Interface i2 : Inherits i1
Sub m2()
End Interface
Class sample
Implements i2
Sub Main()
Dim s As New sample()
s.m1()
s.m2()
End Sub
End Module
Namespaces
Namespaces are used to create logical groups of related classes and interfaces.
Need for Namespace
Namespaces allow us to organize Classes so that they can be easily
accessed in other applications.
Namespaces enable us to avoid any naming conflicts between classes
that have the same name. We can use two classes with the same name in
an application provided they belong to different namespaces.
Creating Namespaces
Every project in VB.Net has a root namespace, which is set in the Property
page of the project. When we create a project, by default, the name of the root
namespace for the project is set to the name of the new project. For example,
the root namespace for a project named MyApp is MyApp.
Namespace College
Class Student
[Members]
End Class
End Namespace
The fully qualified or proper usage of the class Student will be
MyApp.College.Student
Using Namespaces
We can use the Namespace either explicitly through direct addressing or
implicitly through the Imports statement.
Direct addressing involves accessing any class by providing the fully qualified
name. For Example
Dim t as New System.Threading.Thread()
Imports Statement allows us to use a class directly without giving fully
qualified name. For Example
Imports System.Threading
….
Dim t as New Thread()
Imports Statement must appear after any Option statements, if present, but
before any other code. It can be applied only to the namespaces. It cannot be
applied to classes directly. The following Imports statement is incorrect
Imports System.Threading.Thread
Imports MyApp.Perls
Module Module1
Sub Main()
'Accessing using Imports
Website.Execute()
'Accessing using fully qualified name
Ruby.Website.Open()
Console.ReadKey()
End Sub
End Module
Components
A component is a special type of executable built from a .NET project. After
compilation the component is typically referenced by applications needing the
services provided by the component. Thus, the components facilitate
reusability, which is one of the important objectives of object oriented
programming.
Creating the Component
The steps in creating a .NET DLL component are as follows
1. Create a new Application using File->New->Project option
2. The New Project dialog appears. Select Class Library Template and
enter Project Name as MyComponent
3. Change the name of the class from Class1 to College
4. Enter the following code
Public Class College
Dim CollegeName, Location As String
Public Sub New(ByVal c As String, ByVal l As String)
CollegeName = c
Location = l
End Sub
Public Sub Display()
Console.WriteLine("College Name - " & CollegeName)
Console.WriteLine("Location - " & Location)
End Sub
End Class
5. Compile this class as a DLL by clicking Build on the Debug menu
The DLL that results from the build command is placed into the \bin\debug
directory immediately below your .NET project directory. By default, the DLL
has the same name as your component project. For instance, if you named the
project MyComponent in the New Project dialog, the DLL produced by your
project will be named MyComponent.dll. This name is also the default
Namespace name for the classes contained within the project.
Using the Component
The steps in creating an application that uses the above component are
1. Create a new application using File->New->Project option
2. The New Project dialog appears. Select Console Application and enter
Project Name as TestApp
3. In the Solution Explorer, click on the project name and select Add
Reference option. Add Reference dialog appears
4. Click the Browse tab and then browse to locate the component DLL built
in the preceding section
5. Select the MyComponent.dll file, click Open, and then click OK.
The Solution Explorer now shows the ‘MyComponent’ added as a
reference in your application. Now all of the classes, along with their
properties, methods, and events, are now available to your application.
6. Enter the following code and run
Module Module1
Sub Main()
Dim c As New MyComponent.College("SRM", "Potheri")
c.Display()
Console.ReadKey()
End Sub
End Module
Access Modifiers
Access modifiers are an integral part of object-oriented programming. They
support the concept of encapsulation, which promotes the idea of hiding
functionality. Access modifiers allow you to define who does or doesn't have
access to certain features.