Monday, February 13, 2023

What is mean by Shadowing?

Shadowing:

The terms "Shadowing", is defined in VB.Net. In C#, this is known as new keyword.
When the method is defined in the base class, is not able to override and we need to provide a different implementation for the same in the derived class. In this kind of scenario, we can use hide the base class implementation and provide a new implementation using the Shadows(VB.Net)/new(C#) keyword.

Example:

           
Public Class ParentClass
    Public Sub Display()
        Console.WriteLine("Parent class")
    End Sub

End Class

Public Class ChildClass
    Inherits ParentClass

    Public Shadows Sub Display()
        Console.WriteLine("Child class")
    End Sub
End Class

 
 Dim p As New ParentClass
        Dim c As New ChildClass
        Dim pc As ParentClass = New ChildClass
        p.Display()
        c.Display()
        pc.Display()

Output:
Parent class
Child class
Parent class



No comments:

Post a Comment