- Back to Home »
- VB.NET »
- VB.NET Inheritance, Multi level inheritance
Posted by :
Sudhir Chekuri
Saturday, 5 October 2013
VB.NET Inheritance
Inheritance is a process of acquiring properties from parent class to child class.
Inherits is the keyword used for inheritance.
VB.NET Inheritance example program
Module Module1
Public Class class1
Public Sub print()
Console.WriteLine("print")
End Sub
End Class
Public Class class2
Inherits class1
Public Sub display()
Console.WriteLine("display")
End Sub
End Class
Sub Main()
Dim o As New class1()
o.print()
Dim c As New class2
c.print()
c.display()
Console.ReadLine()
End Sub
End Module
Output
print
print
display
Explanation
class1 contain print method
class2 contain display method
class2 inherits class1
print and display methods can be called using object of class2.
VB.NET Multi level Inheritance program
Module Module1
'create base class
Public Class class1
Public i As String = "Hai"
Public Sub print1()
Console.WriteLine("I value is " & i)
End Sub
End Class
'create derived class
Public Class class2
Inherits class1
Public Sub print2()
Console.WriteLine("I value is " & i)
End Sub
End Class
'new derived class
Public Class class3
Inherits class2
Public Sub print3()
Console.WriteLine("I value is " & i)
End Sub
End Class
Sub Main()
Dim c1 As New class3()
c1.print1()
c1.print2()
c1.print3()
Console.ReadLine()
End Sub
End Module
Output
I value is Hai
I value is Hai
I value is Hai
Inheritance is a process of acquiring properties from parent class to child class.
Inherits is the keyword used for inheritance.
VB.NET Inheritance example program
Module Module1
Public Class class1
Public Sub print()
Console.WriteLine("print")
End Sub
End Class
Public Class class2
Inherits class1
Public Sub display()
Console.WriteLine("display")
End Sub
End Class
Sub Main()
Dim o As New class1()
o.print()
Dim c As New class2
c.print()
c.display()
Console.ReadLine()
End Sub
End Module
Output
display
Explanation
class1 contain print method
class2 contain display method
class2 inherits class1
print and display methods can be called using object of class2.
VB.NET Multi level Inheritance program
Module Module1
'create base class
Public Class class1
Public i As String = "Hai"
Public Sub print1()
Console.WriteLine("I value is " & i)
End Sub
End Class
'create derived class
Public Class class2
Inherits class1
Public Sub print2()
Console.WriteLine("I value is " & i)
End Sub
End Class
'new derived class
Public Class class3
Inherits class2
Public Sub print3()
Console.WriteLine("I value is " & i)
End Sub
End Class
Sub Main()
Dim c1 As New class3()
c1.print1()
c1.print2()
c1.print3()
Console.ReadLine()
End Sub
End Module
Output
I value is Hai
I value is Hai
I value is Hai
it's an informative article but explaination is missing for a little bit.Please work ok it for stuents
ReplyDelete