FROM THEORY TO PRACTICEEXTENDING OUR CLASS
InheritanceBase Class              Student       Graduate    Derived Class
Create Properties                                  Inherits Keyword Private Property   Property Procedure
Creating the Object
Using the Object
...MORE ON INHERITANCE
Class Person  Public Name As String  Public Address As String  Public City As StringEnd Class
Public Class Customer  Public Name As String  Public Address As String  Public City As StringEnd ClassPublic Class Employe...
VB.NET Supports SingleInheritancePublic Class Programmer  Inherits Employee  Public Project As StringEnd ClassPublic Class...
Inheritance Hierarchy In the previous example Programmer class  contains members defined in its immediate  base class, Em...
Protected Accessibility A derived Class does not have access to its  base classes’ Private Members Private Members can o...
Con’tdClass Student  Private strStudentID As String  Protected CourseTitle As StringEnd Class
Cont’dClass Guest  Inherits UserSub New ( )  ‘Results in an Error message  ‘Error:strStudentID is private to Student  strS...
...POLYMORPHISM
Overriding If a method or base class does not fit the  requirements of the same methods in the derived  class you can cre...
An ExamplePublic Class ParentClass  Public Overridable Sub ParentMethod()     MsgBox(“Hello World from parent”)  End SubEn...
Preventing InheritancePublic NotInheritable Employee()
Oveloading Methods One of the most powerful new polymorphic  features in VB.NET is the ability to overload a  method Ove...
Overloading Methods Cont’dPublic Sub Method1(i As integer, j As  Integer) The parameter list of the method above can be  ...
Overloading Methods Cont’dPublic Function SearchEmployees(ByRef Name  _ As String) As Boolean„Code to search through the d...
Overloading If you wanted to search for Employees by age?? VB 6? VB.NET?Public Function SearchEmployees (ByRef Age As _...
Terminating In VB6 an object was destroyed when its  reference counter was decremented to zero VB.NET does not use refer...
Garbage Collection How it works is quite simple Scans system for references Removes problem of circular references and ...
Finalize Method Garbage collection is similar to VB6  Class_Terminate Event As object is being terminated the garbage  c...
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)
Upcoming SlideShare
Loading in …5
×

Is2215 lecture3 student (1)

368 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
368
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Is2215 lecture3 student (1)

  1. 1. FROM THEORY TO PRACTICEEXTENDING OUR CLASS
  2. 2. InheritanceBase Class Student Graduate Derived Class
  3. 3. Create Properties Inherits Keyword Private Property Property Procedure
  4. 4. Creating the Object
  5. 5. Using the Object
  6. 6. ...MORE ON INHERITANCE
  7. 7. Class Person Public Name As String Public Address As String Public City As StringEnd Class
  8. 8. Public Class Customer Public Name As String Public Address As String Public City As StringEnd ClassPublic Class Employee Public Name As String Public Addresss As String Public City As StringEnd Class
  9. 9. VB.NET Supports SingleInheritancePublic Class Programmer Inherits Employee Public Project As StringEnd ClassPublic Class Manager Inherits Employee Public intMangedEmployees As IntegerEnd Class
  10. 10. 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
  11. 11. 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
  12. 12. Con’tdClass Student Private strStudentID As String Protected CourseTitle As StringEnd Class
  13. 13. Cont’dClass Guest Inherits UserSub New ( ) ‘Results in an Error message ‘Error:strStudentID is private to Student strStudentID= “90000000” ‘This will work CourseTitle = “BIS”End Sub
  14. 14. ...POLYMORPHISM
  15. 15. 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
  16. 16. An ExamplePublic Class ParentClass Public Overridable Sub ParentMethod() MsgBox(“Hello World from parent”) End SubEnd ClassPublic Class ChildClass Inherits ParentClass Public Overrides Sub ParentMethod() msgBox(“Hello World from child”) End SubEnd Class
  17. 17. Preventing InheritancePublic NotInheritable Employee()
  18. 18. 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.
  19. 19. Overloading Methods Cont’dPublic 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)
  20. 20. Overloading Methods Cont’dPublic 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:
  21. 21. 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
  22. 22. 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
  23. 23. 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()
  24. 24. Finalize Method Garbage collection is similar to VB6 Class_Terminate Event As object is being terminated the garbage collection will call its Finalize methodProtected Overrides Sub Finalize ()‘Clean up code hereEnd SubMyStudent = Nothing

×