Is2215 lecture3 student (1)

27
EXTENDING OUR CLASS FROM THEORY TO PRACTICE

description

 

Transcript of Is2215 lecture3 student (1)

Page 1: Is2215 lecture3 student (1)

EXTENDING OUR CLASSFROM THEORY TO PRACTICE

Page 2: Is2215 lecture3 student (1)

Inheritance

Student

Graduate

Base Class

Derived Class

Page 3: Is2215 lecture3 student (1)

Create Properties

Inherits Keyword

Private Property

Property Procedure

Page 4: Is2215 lecture3 student (1)

Creating the Object

Page 5: Is2215 lecture3 student (1)

Using the Object

Page 6: Is2215 lecture3 student (1)

MORE ON INHERITANCE...

Page 7: Is2215 lecture3 student (1)
Page 8: Is2215 lecture3 student (1)

Class Person Public Name As StringPublic Address As StringPublic City As String

End Class

Page 9: Is2215 lecture3 student (1)

Public Class CustomerPublic Name As StringPublic Address As StringPublic City As String

End ClassPublic Class Employee

Public Name As StringPublic Addresss As StringPublic City As String

End Class

Page 10: Is2215 lecture3 student (1)
Page 11: Is2215 lecture3 student (1)

VB.NET Supports Single Inheritance

Public Class ProgrammerInherits EmployeePublic Project As String

End Class

Public Class ManagerInherits EmployeePublic intMangedEmployees As Integer

End Class

Page 12: Is2215 lecture3 student (1)
Page 13: Is2215 lecture3 student (1)

Inheritance Hierarchy

In the previous example Programmer class contains members defined in its immediate base class, Employee

Also members defined in the Employee’s base class, Person

Page 14: Is2215 lecture3 student (1)

Protected Accessibility

A derived Class does not have access to its base classes’ Private Members

Private Members can only be accessed in the immediate Class in which they are defined

Protected Members can however be accessed within an inheritance hierarchy

Page 15: Is2215 lecture3 student (1)

Con’td

Class StudentPrivate strStudentID As StringProtected CourseTitle As String

End Class

Page 16: Is2215 lecture3 student (1)

Cont’d

Class GuestInherits User

Sub New ( )‘Results in an Error message‘Error:strStudentID is private to StudentstrStudentID= “90000000”‘This will work

CourseTitle = “BIS”End Sub

Page 17: Is2215 lecture3 student (1)

POLYMORPHISM...

Page 18: Is2215 lecture3 student (1)

Overriding

If a method or base class does not fit the requirements of the same methods in the derived class you can create a new method with the same name that performs different actions

VB supports Polymorphism through overrides

You can override a method in the base Class by using the Overridable keyword to declare it

You can then declare the same method in the derived class but give it different functionality

Page 19: Is2215 lecture3 student (1)

An Example

Public Class ParentClassPublic Overridable Sub ParentMethod()

MsgBox(“Hello World from parent”)End Sub

End Class

Public Class ChildClassInherits ParentClassPublic Overrides Sub ParentMethod()

msgBox(“Hello World from child”) End SubEnd Class

Page 20: Is2215 lecture3 student (1)

Preventing Inheritance

Public NotInheritable Employee()

Page 21: Is2215 lecture3 student (1)

Oveloading Methods

One of the most powerful new polymorphic features in VB.NET is the ability to overload a method

Overloading means that we can declare a method of the same name more than once in a class once we provide it with a new parameter list

A different parameter list in this case means different data types in the list or a different number of arguments.

Page 22: Is2215 lecture3 student (1)

Overloading Methods Cont’d

Public Sub Method1(i As integer, j As Integer)

The parameter list of the method above can be thought of as (integer, integer)

To overload this method we need to come up with a different paramater list e.g:(string, decimal)(integer, integer, string)

Page 23: Is2215 lecture3 student (1)

Overloading Methods Cont’d

Public Function SearchEmployees(ByRef Name _ As String) As Boolean

‘Code to search through the databaseMessageBox.Show(“Employee Found”)End Function

Arguments are passed to method ByVal as default

Perhaps in our application we created a Employee class and we wish to search for employees by employee name:

Page 24: Is2215 lecture3 student (1)

Overloading

If you wanted to search for Employees by age??

VB 6? VB.NET?

Public Function SearchEmployees (ByRef Age As _ Integer) As Boolean

‘Code to Search DatabaseMessageBox.Show (“Employee Found”)End Function

Page 25: Is2215 lecture3 student (1)

Terminating

In VB6 an object was destroyed when its reference counter was decremented to zero

VB.NET does not use reference counting to determine when an object should be destroyed it uses garbage collection

Page 26: Is2215 lecture3 student (1)

Garbage Collection

How it works is quite simple Scans system for references Removes problem of circular

references and objects living when they should have been destroyed

Performance gain, scan when system is idle

Can be called explicitlySystem.GC.Collect()

Page 27: Is2215 lecture3 student (1)

Finalize Method

Garbage collection is similar to VB6 Class_Terminate Event

As object is being terminated the garbage collection will call its Finalize method

Protected Overrides Sub Finalize ()‘Clean up code hereEnd SubMyStudent = Nothing