Download - Apply Inheritance and Polymorphism
-
7/28/2019 Apply Inheritance and Polymorphism
1/21
Chapter 4.2
APPLY INHERITANCE AND POLYMORPHISM
-
7/28/2019 Apply Inheritance and Polymorphism
2/21
What is Inheritance?
Inheritance is the property in which a derived class
acquires the attributes of its base class.
Recall that classes contain
Fields
Properties
Event
Methods
-
7/28/2019 Apply Inheritance and Polymorphism
3/21
What is Inheritance?
Inheritance implies at least two classes
Class play role of parent (superclass)
Class play role of child (subclass) To implement inheritance, use the Inherits keyword.
-
7/28/2019 Apply Inheritance and Polymorphism
4/21
Basic Inheritance Statement
Simplest inheritance relationship
Public Class Parent From listing you can see
End Class Inherits Parent is all thatwe must add to indicate
Public Class Child inheritance of
Inherits Parent child from parent.
End Class
-
7/28/2019 Apply Inheritance and Polymorphism
5/21
Inheritance
When user create class, new class can be based onanother class.
User can create new class inherit from one of
existing .NET classes or from one of user own
classes. Example:
Partial Public Class Form1
Inherits System.Windows.Forms.Form
The Inherits statement must follow the class header priorto any comments.
Public class NewClass
Inherits BaseClass
C B C
-
7/28/2019 Apply Inheritance and Polymorphism
6/21
Ca ng t e Base C ass
Constructor
Constructor in Inheritance Subclass cannot inherit constructor from base class.
Each class must have its own constructor.
Calling base class constructor
With this statemen MyBase.NEW()
Example:
Sub New(ByVal title As String, ByVal price By Decimal)
assign property values
call the base class constructor
MyBase.NEW(title, price)
End Sub
-
7/28/2019 Apply Inheritance and Polymorphism
7/21
Important Of Inheritance
Provides re-usability of code.
Help in reducing the code size and better project
management.
Use Inherits keyword to inherit a class into otherclass.
Old class called Base class and new class called
Derived class
Use mybase keyword to reference the mambers of
base class.
-
7/28/2019 Apply Inheritance and Polymorphism
8/21
Polymorphism
Polymorphism is one of the crucial features of OOP.
An object can be treated as if it were a different kind
of object provided common sense prevails. 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 withdifferent argument list.
-
7/28/2019 Apply Inheritance and Polymorphism
9/21
Example:
Sub Main()
Dim two As New One()
WriteLine(two.add(10))
'calls the function with one argument
WriteLine(two.add(10, 20))
'calls the function with two arguments
WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Read()End Sub
-
7/28/2019 Apply Inheritance and Polymorphism
10/21
Implement Polymorphism
Class ParentPublic Overridable Sub p1()
Console.Write("Parent.p1")
End Sub
Public Sub p2()MyClass.p1()
'Implementing keyword MyClass here tells all
derived classes to refer to the base / abstract
class wherein the keyword MyClass appearsEnd Sub
End Class
-
7/28/2019 Apply Inheritance and Polymorphism
11/21
Implement Polymorphism
Implementing the keyword MyClass in the baseclass tells all derived classes to use the method in
the base class itself and, therefore, a stack overflow
error is prevented.
p.p2() 'stack overflow error is preventedc = New Child()
c.p1() 'stack overflow error is prevented
c.p2() 'stack overflow error is prevented
End Sub
-
7/28/2019 Apply Inheritance and Polymorphism
12/21
Chapter 4.3
Construct String
-
7/28/2019 Apply Inheritance and Polymorphism
13/21
Declare and Manipulate char type
The String Class represents character strings.
The String data type comes from the System.Str ing
class
The String type represents a string ofUnicode
Characters .
The String class is a sealed class , so you cannot
inherit another class from the String class.
-
7/28/2019 Apply Inheritance and Polymorphism
14/21
String References as Parameters
What is Parameters? A constructor that contains an argument list; as opposed
to an empty constructor.
VB.NET String.Format()
VB.NET String Format method replace the argumentObject into a text equivalent System.String.
System .Format(ByVal form at As String, ByVal arg0 As
Object) As String
Parameters:String format : The format String
-
7/28/2019 Apply Inheritance and Polymorphism
15/21
VB.NET String.Length()
The Length() function in String Class returned the
number of characters occurred in a String.
Sys tem.String .Leng th() As Integer
Returns: Integer : The number of characters in the specified String
For exmple:
"This is a Test".Length() returns 14.
-
7/28/2019 Apply Inheritance and Polymorphism
16/21
VB.NET String.Concat()
Concat in VB.NET String Class using for concat twospecified String Object
System.Str ing .Concat(ByVal str1 As Str ing,
ByVal str2 As Str ing ) As Str ing
String Concat method returns a new String
Parameters:
String str1 : Parameter String
String str2 : Parameter String
Returns:
String : A new String retrun with str1 Concat with str2
-
7/28/2019 Apply Inheritance and Polymorphism
17/21
VB.NET String.substring()
Substring in Vb.Net String Class returns a new string
that is a substring of this string. The substring begins atthe specified given index and extended up to the givenlength.
Publ ic Func t ion Substr ing (ByVal star t Index As
Integer, ByVal leng th As Integer) As Str ing
Parameters: startIndex: The index of the start of the substring.
length: The number of characters in the substring.
Returns:
The specified substring. Exceptions:
System.ArgumentOutOfRangeException: the beginIndex orlength less than zero, or the begin index + length not within thespecified string
-
7/28/2019 Apply Inheritance and Polymorphism
18/21
Chapter 4.4
Exception Handling
-
7/28/2019 Apply Inheritance and Polymorphism
19/21
Exception Handling in VB.NET
What is exception?
An error that occurs at run time.
Exception Handling?
Exception handling is an in built mechanism in .NET
framework to detect and handle run time errors.
Cause of error?
Logic errors.
System errors.
VB.NET provides three keywords try, catch andfinally to do exception handling.
-
7/28/2019 Apply Inheritance and Polymorphism
20/21
The general form try-catch
Try
statement that may cause error
Catch [VariableName As ExceptionType]
statement for action when exception occurs
[Finallystatements that always execute before exit of
Try block]
End Try
-
7/28/2019 Apply Inheritance and Polymorphism
21/21
Example
Public Sub MyException(ByVal a As Int32, ByVal b As Int32)
Dim Result As Int32Try
'The try block from where an exception will thrown
Result = a / b
textBox3.Text = Result.ToString()Catch e As System.DivideByZeroException
'The catch block where Exception will be handle
'Here I am using e.Message
MessageBox.Show(e.Message)End Try
End Sub