Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Unit III c

Overloading, Overriding, Shadowing,


Exception handling
Polymorphism
• Polymorphism means one object several
forms.
• One object executes different task at different
instances
• Types:
• Compile time polymorphism
• Runtime polymorphism
Compile time Polymorphism
• In compile time polymorphism the decisions
are made at compile time
• An example of compile time polymorphism is
method overloading
• Where compiler already knows which
overloaded method is going to be called.
• Overloads keyword is used to overload a
method or function.
Example
Public Class Calculate
Dim res As Double
Public Overloads Sub area(ByVal r As Double)
res = 3.14 * r * r
Console.WriteLine("Area of circle " & res)
End Sub
Public Overloads Sub area(ByVal l As Integer, ByVal b As Integer)
res = l * b
Console.WriteLine("Area of rectangle " & res)
End Sub
End Class

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

Sub G(ByVal I as Integer)


End Sub
End Class

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

End Sub Sub Main()


Sub square(ByVal n As Integer)
Dim s As Integer
Dim d As New derived()
s=n*n d.circle()
Console.WriteLine("Square is" & s) Console.ReadKey()
End Sub End Sub
End Class
End Module
Output
Exception Handling
• Exception is an error occur at runtime
• Occurrence of error at runtime can lead to
program termination
• An error is an event and object created by this
event is called exception
• Difference between error and exception is that
exceptions are those which can be handled at
runtime
Types of errors
1. Syntax Error:
• Syntax error are detected at compile time
• It is also called as Compilation error
• When the rules of programming are violated
compilation error is occurred
• It is easy to detect and correct these errors
Eg: Dim integer as Integer
2 Runtime Errors:
• A runtime error is detected at runtime of a program execution
• These are also called as Exceptions
• Divide by Zero, NullPointerException are examples of runtime
error
• Runtime error occurs when a code of program attempts an
action which is impossible to perform
• Eg:
Public A as Integer
Public B as Integer
Public C as Integer
A=1
B=0
C=A/B
3 Logical errors:
• The programmer code runs without any
syntax or any runtime error but it gives
unexpected result this type of error is called
logical error
• It occurs due to applying wrong logic to
implement the code
Handling Exception
• When exception is occurs the program is
terminated
• To avoid this we have to handle the exception
explicitly
• A generalize class exception is used to handle
exceptions
Syntax
Try
– Code for Exception handler
Catch ex As Exception
Finally
‘statements that must be executed
End try
In vb.net exceptions are handled using 4
keywords
1 Try: In this block we have to write the code
where exceptions may occur
2 catch: contains the code which handle the
exceptions thrown by try block
3 Throw : An exception can be thrown by using
keyword ‘Throw’ when it is detected
4 Finally: the part of code which is followed by
finally keyword always executed even if the
exception is not handled.
Example
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim n1, n2, div As Integer
n1 = TextBox1.Text
n2 = TextBox2.Text
Try
div = n1 / n2
MsgBox("div is" & div)

Catch ex As Exception
MsgBox("Division by 0")
End Try
End Sub
End Class
Output

You might also like