Inheritance and polymorphism in VB.Net

In this article we will learn how to Inheritance and polymorphism in VB.Net.
  • 5846
 

Inheritance and polymorphism

Inheritance and Polymorphism both are the part of object oriented programming (OOP Concept). Now here we define each individual.

INHERITANCE:

Inheritance is the concept that when a class of  objects  is defined, any subclass that is defined can inherit the definitions of one or more general classes. This means for the programmer that an object in a subclass need not carry its own definition of data and methods that are generic to the class (or classes) of which it is a part.As the name inheritance suggests an object is able to inherit characteristics from another object. In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work the objects need to have characteristics in common with each other.

Polymorphism:

Polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

Polymorphism is one of the crucial features of OOP. It means  " one name multiple form". 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.

Code behind a Program Related to Inheritance and polymorphism.

Class Tester
    Shared Sub Main()
        Dim point1, point2 As Point
        Dim circle1, circle2 As Circle
        point1 = New Point(30, 50)
        circle1 = New Circle(120, 89, 2.7)
        Console.WriteLine("Point point1: " & point1.ToString() & _
           vbCrLf & "Circle circle1: " & circle1.ToString())
        point2 = circle1
        Console.WriteLine("Circle circle1 (via point2): " & point2.ToString())
        circle2 = CType(point2, Circle) ' allowed only via cast
 
        Console.WriteLine("Circle circle1 (via circle2): " & circle2.ToString())
        If (TypeOf point1 Is Circle) Then
            circle2 = CType(point1, Circle)
            Console.WriteLine("cast successful")
        Else
            Console.WriteLine("point1 does not refer to a Circle")
        End If
        Console.ReadLine()
    End Sub

End
Class
Public
Class Point
    Private mX, mY As Integer
 
    Public Sub New()
    End Sub ' New
 
    Public Sub New(ByVal xValue As Integer, _
       ByVal yValue As Integer)
    End Sub ' New
    Public Overrides Function ToString() As String
        Return "[" & mX & ", " & mY & "]"
    End Function ' ToString

End
Class
Public
Class Circle
    Inherits Point 

    Private mRadius As Double 
    Public Sub New()
    End Sub ' New
 
    Public Sub New(ByVal xValue As Integer, _
       ByVal yValue As Integer, ByVal radiusValue As Double)
        MyBase.New(xValue, yValue)
    End Sub ' New
 
    Public Overrides Function ToString() As String
        Return "Center= " & MyBase.ToString() & _
           "; Radius = " & mRadius
    End Function ' ToString

End
Class

Output of the application

poly.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.