Unit III C: Overloading, Overriding, Shadowing, Exception Handling
Unit III C: Overloading, Overriding, Shadowing, Exception Handling
Module Module1
Sub Main()
Dim a As New Calculate()
a.area(4.3)
a.area(4, 5)
Console.ReadKey()
End Sub
End Module
Output
Runtime Polymorphism
• In this decisions are made at runtime i.e which
method is to be called is decided at run time.
• This is achieved by method overriding
• Method overriding provides the ability to
derived class to use the same method name as
in the base class to perform different
functionality.
Example
Public Class emp
Dim name As String
Dim address As String
Public Overridable Sub showinfo()
Module Module1
Console.WriteLine("Name " & name)
Console.WriteLine("address " & address) Sub Main()
End Sub Dim t As New empinfo
End Class
t.showinfo()
Public Class empinfo
Inherits emp Console.ReadKey()
Dim empid As Integer
Dim sal As Integer
Public Overrides Sub showinfo() End Sub
MyBase.showinfo()
Console.WriteLine("emp id " & empid)
Console.WriteLine("emp sal" & sal)
End Sub
End Class
Modifiers
• Modifiers are used to override the methods
1 Overridable keyword: it allows the method in base class
to be overridden in its derived class.
2 Overrides keyword: it is used to override the method
which is declared as overridable method in base class.
3 Non Overridable keyword: to prevent the method from
overriding this keyword is used in base class
4 Must Overridable keyword: if any method in base class
is declared as must override then it must be overridden
in its derived class.
Difference between Method Overloading and
Method Overriding
Parameters Method Overloading Method Overriding
Concept Overloading allows the Overriding allows derived
methods of same class to class to redefine the
share same name but method of base class using
every method as different same method signature
signature
polymorphism Compile time Run time polymorphism is
polymorphism is achieved achieved through method
through method overriding
overloading
Access Specifiers A method which is going to A method which is going to
be overloaded can have be override must have
any access specifier access specifier as Public
Need of inheritance Method overloading Method overriding need
doesn’t need inheritance inheritance
Shadowing
• It is a feature when 2 programming elements
share same name one of them can be hide or
shadow the other one.
• Shadowing is use to hide base class methods
• It doesn’t remove inherited type member with
that name ,it just make that name unavailable in
derived class
• The main purpose is to protect the definition of
class members.
Types of shadowing
• Entities that can be overloaded can choose are of 2
types of shadowing
1 Shadowing by Name: is specified using shadow
keyword
An entity that shadows by name hides everything but
that name in the base class including all overloads
2 Shadowing by Name & Signature: is specified using the
overloads keyword
an entity that shadows by name & signature, hides
everything by that name same signature as the entity.
Class Base
Example
Sub F()
End Sub Sub Main()
Dim x as New Derived()
Sub F(ByVal I as Integer)
x.F() ‘calls base class F()
End Sub
x.G()‘error:No such method
Sub G()
x.G(5) ‘error:derived class
End Sub End Sub
Class Derived
Inherits Base
Overloads Sub F(ByVal I as integer) ’ only hides F(ByVal I as Integer)
End Sub
Shadows Sub G(ByVal I as Integer) ‘hides G() and G(ByVal I as Integer)
End Sub
End Class
Example
Class Derived
Class Base
Sub circle()
Inherits Base
Console.WriteLine(" I am in Circle") Overloads Sub circle(ByVal r As Integer)
End Sub End Sub
Sub cirlcle(ByVal r As Integer)
Dim res As Integer
res = 3.14 * r * r
Shadows Sub square(ByVal n As Integer)
Console.WriteLine("Area of circle is" & res) End Sub
End Class
End Sub
Sub square()
Console.WriteLine(" I am in rectangle") Module Module1
Catch ex As Exception
MsgBox("Division by 0")
End Try
End Sub
End Class
Output