Вы находитесь на странице: 1из 21

APPLY INHERITANCE AND POLYMORPHISM

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

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.

Basic Inheritance Statement


Simplest inheritance relationship
Public Class Parent End Class Public Class Child Inherits Parent End Class From listing you can see Inherits Parent is all that we must add to indicate inheritance of child from parent.

Inheritance
When user create class, new class can be based on

another 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 prior to any comments. Public class NewClass Inherits BaseClass

Calling the Base Class 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

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 other class. Old class called Base class and new class called Derived class Use mybase keyword to reference the mambers of base class.

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 with different argument list.

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

Implement Polymorphism
Class Parent

Public 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 appears End Sub End Class

Implement Polymorphism
Implementing the keyword MyClass in the base class

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 prevented c = New Child() c.p1() 'stack overflow error is prevented c.p2() 'stack overflow error is prevented End Sub

Construct String

Declare and Manipulate char type


The String Class represents character strings.

The String data type comes from the System.String

class The String type represents a string of Unicode Characters . The String class is a sealed class , so you cannot inherit another class from the String class.

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 argument Object into a text equivalent System.String. System.Format(ByVal format As String, ByVal arg0 As Object) As String Parameters: String format : The format String

VB.NET String.Length()
The Length() function in String Class returned the

number of characters occurred in a String. System.String.Length() As Integer Returns:


Integer : The number of characters in the specified

String

For exmple: "This is a Test".Length() returns 14.

VB.NET String.Concat()
Concat in VB.NET String Class using for concat two

specified String Object System.String.Concat(ByVal str1 As String, ByVal str2 As String) As String 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

VB.NET String.substring()
Substring in Vb.Net String Class returns a new string

that is a substring of this string. The substring begins at the specified given index and extended up to the given length. Public Function Substring(ByVal startIndex As Integer, ByVal length As Integer) As String 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 or

length less than zero, or the begin index + length not within the specified string

Exception Handling

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 and finally

to do exception handling.

The general form try-catch


Try
statement that may cause error Catch [VariableName As ExceptionType] statement for action when exception occurs [Finally statements that always execute before exit of Try block] End Try

Example
Public Sub MyException(ByVal a As Int32, ByVal b As Int32) Dim Result As Int32 Try '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

Вам также может понравиться