Inheritance in VB.NET

In this article we discuss about Inheritance.
  • 2382

Inheritance is a fundamental Object Oriented concept

A class can be defined as a "subclass" of another class.

  • The subclass inherits all data attributes of its superclass
  • The subclass inherits all methods of its superclass
  • The subclass inherits all associations of its superclass
The subclass can:
 
  • Add new functionality
  • Use inherited functionality
  • Override inherited functionality
Functionality of Inheritance:

fi.gif

Code of above Inheritance Diagram:

Public Module Student
    Public collegename As [String] = "MCN"
    Public rollno As Integer
    Public name As [String]
    Public Sub insert()
        Console.WriteLine("plz fill student information"
        Console.WriteLine("student roll no")
        rollno = Integer.Parse(Console.ReadLine())|
        Console.WriteLine("student name")
        name = Console.ReadLine()
    End Sub
    Public Sub show()
        Console.Write("*******************************************")
        Console.WriteLine("studen rollno" & "student name" & "student college")
        Console.WriteLine(rollno & name & collegename)
    End Sub

End
Module

public Module Students Inherits Student
    Public totalmarks As Integer = 200

    Private getmarks As Integer
    Public Sub [get]()
        getmarks = 120
        Console.WriteLine("max marks")
        Console.WriteLine(totalmarks)
        Console.WriteLine("get marks")
        Console.Write("********************************************************")
        Console.WriteLine(getmarks)
    End Sub

End
Module

Module
output
    Sub Main()
        Dim s1 As New Students()
        s1.insert()
        s1.show()
        s1.[get]()
    End Sub

End Module

Output:

gh.gif

Inheritance Hierarchy

 class-tree.gif

  • Each class has one (and only one) super class.
  • Inheritance creates a class hierarchy
  • Classes higher in the hierarchy are more general and more abstract
  • There is no limit to the depth of the class tree
© 2020 DotNetHeaven. All rights reserved.