2. 2
What is inheritance?
Inheritance can be defined as the feature of the programming language that offers an
opportunity to make the use of functions that are already defined in a base class.
In Inheritance, the base class is the class which passes its functionality to other class. It
is also called the parent class sometimes.
The class that inherits the base class is called derived class and sometimes called child
class as well.
As the word inheritance itself demonstrates, it facilitates the passing of values or
functions to some other classes. In VB.Net we can inherit more than one class to the
derived class simultaneously.
3. 3
What is inheritance?
The purpose of inheritance is to make reduce the size of code and avoid defining the
same function multiple times.
Inheritance may be defined as the property of programming language that implements
the mechanism of reusing the already defined functionality.
It is very useful when it comes to developing an application with efficient code.
There is no need to write the same function over and over.
It could be used several times based on the requirement.
4. 4
Advantages of Inheritance
The feature of inheritance is very beneficial in programming. It actually helps a lot while
writing complex programs as the feature of passing the functionality mitigates the complex
looks of the code. Below are some of its common Advantages
Reduces ambiguity – The code once is written could be used several times. Hence it
eliminates the duplicates and makes the code unambiguous.
Reusability – It follows the rule of creating once and use multiple times. Once any
function is defined, we can use that function n number of times based on our
requirement.
Eliminate Coding Complexity – The code has to be written precisely in order to follow
proper documentation and make it easy for other developers to understand the code. It
could be done with the help of Inheritance.
Enhances program efficiency – The program that makes the use of inheritance are very
efficient as there are a few numbers of lines as compared to the same program which
was developed without inheritance.
5. 5
Syntax for implementing inheritance
[access specifier] Class [Base class name] Statement 1
Statement 2
..
Statement n
End Class
Class [Derived class name] Inherits [Base class name] Statement 1
Statement 2
..
Statement n
End Class
Here, the based classed has to be defined first. Then by making the use of the “Inherits”
keyword, we can implement inheritance in the program. So basically we need at least two
classes to take leverage of inheritance. Once the class has to be the base class while
others will be the derived class.
6. 6
Example for implementing inheritance
Module module1
Class x
Public name As String
Public roll As Integer
Public Sub get_data()
Console.WriteLine("Enter Name: ")
name = Console.ReadLine()
Console.WriteLine("Enter Roll ")
roll = Console.ReadLine()
End Sub
End Class
Class y : Inherits x
Public Sub display_data()
Console.WriteLine("You have entered following details:")
Console.WriteLine("Name: {0}", name)
Console.WriteLine("Roll: {0}", roll)
End Sub
End Class
Sub main()
Dim obj As New y()
obj.get_data()
obj.display_data()
Console.ReadKey()
End Sub
End Module
7. 7
Inheriting Constructor and Destructors
Module module1
Class x
Public Sub New()
Console.WriteLine("Constructor of Base Class")
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Destructor of Base class called")
End Sub
End Class
Class y : Inherits x
End Class
Sub main()
Dim obj As New x()
Dim obj1 As New y()
Console.ReadKey()
End Sub
End Module
Constructors and Destructor are, by default, inherited by derived classes.
Module module1
Class x
Public Sub New()
Console.WriteLine("Constructor of Base Class")
End Sub
End Class
Class y
Inherits x
'End Class
Sub main()
Dim obj As New x()
Dim obj1 As New y()
Console.ReadKey()
End Sub
End Module
8. 8
Abstract and Final Class
An abstract class is a class that cannot be instantiated.
An abstract class cannot be used to create objects.
It is generally used as a template to derive other classes.
An abstract class typically has abstract methods which are only method declarations
Without any implementation.
A final class is a class that cannot be used as a base class. This means a final class
cannot be used to derive classes or cannot be inherited.
A class cannot be both abstract as well as final.
In VB.NET all classes are inheritable. The MustInherit keyword makes the base class an
abstract class.
Final Classes in VB.NET are marked with NotInheritable Keyword. They cannot have
derived classes but can be used to create objects.
9. 9
Example of Abstract Class
Module module1
MustInherit Class Absbase
Public Sub test()
Console.WriteLine("Abstract Base Class")
End Sub
MustOverride Sub anothertest()
End Class
Class child
Inherits Absbase
Public Overrides Sub anothertest()
Console.WriteLine("Implementing abstract methods")
End Sub
End Class
Sub main()
Dim obj As New child()
obj.test()
obj.anothertest()
Console.ReadKey()
End Sub
End Module
10. 10
Example of Final Class
Module module1
NotInheritable Class A
Public Sub test()
Console.WriteLine("Parameter not passes")
End Sub
Public Sub test(x As Integer)
Console.WriteLine("Value :", x)
End Sub
End Class
Sub main()
Dim obj As New A()
obj.test()
obj.test(100)
Console.ReadKey()
End Sub
End Module