SlideShare a Scribd company logo
1 of 27
FROM THEORY TO PRACTICE

EXTENDING OUR CLASS
Inheritance

Base 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 String
End Class
Public Class Customer
  Public Name As String
  Public Address As String
  Public City As String
End Class
Public Class Employee
  Public Name As String
  Public Addresss As String
  Public City As String
End Class
VB.NET Supports Single
Inheritance
Public Class Programmer
  Inherits Employee
  Public Project As String
End Class

Public Class Manager
  Inherits Employee
  Public intMangedEmployees As Integer
End Class
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
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
Con’td

Class Student
  Private strStudentID As String
  Protected CourseTitle As String
End Class
Cont’d

Class Guest
  Inherits User
Sub New ( )
  ‘Results in an Error message
  ‘Error:strStudentID is private to Student
  strStudentID= “90000000”
  ‘This will work
  CourseTitle = “BIS”
End Sub
...

POLYMORPHISM
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
An Example
Public Class ParentClass
  Public Overridable Sub ParentMethod()
     MsgBox(“Hello World from parent”)
  End Sub
End Class

Public Class ChildClass
  Inherits ParentClass
  Public Overrides Sub ParentMethod()
     msgBox(“Hello World from child”)
  End Sub
End Class
Preventing Inheritance

Public NotInheritable Employee()
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.
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)
Overloading Methods Cont’d
Public Function SearchEmployees(ByRef Name
  _ As String) As Boolean
„Code to search through the database
MessageBox.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:
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 Database
MessageBox.Show (“Employee Found”)
End Function
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
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 explicitly
System.GC.Collect()
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 here
End Sub
MyStudent = Nothing

More Related Content

What's hot

Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
Oops concept in c++ unit 3 -topic 4
Oops concept in c++ unit 3 -topic 4Oops concept in c++ unit 3 -topic 4
Oops concept in c++ unit 3 -topic 4MOHIT TOMAR
 
Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsEPAM
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Object Oriented Software Development revision slide
Object Oriented Software Development revision slide Object Oriented Software Development revision slide
Object Oriented Software Development revision slide fauza jali
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingHaris Bin Zahid
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaGlenn Guden
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming conceptsrahuld115
 
Advanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, IdiomsAdvanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, IdiomsClint Edmonson
 
Encapsulation of operations, methods & persistence
Encapsulation of operations, methods & persistenceEncapsulation of operations, methods & persistence
Encapsulation of operations, methods & persistencePrem Lamsal
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMd. Tanvir Hossain
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsSanmatiRM
 

What's hot (20)

Oops in vb
Oops in vbOops in vb
Oops in vb
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Oops concept in c++ unit 3 -topic 4
Oops concept in c++ unit 3 -topic 4Oops concept in c++ unit 3 -topic 4
Oops concept in c++ unit 3 -topic 4
 
Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real Projects
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
General OOP concept [by-Digvijay]
General OOP concept [by-Digvijay]General OOP concept [by-Digvijay]
General OOP concept [by-Digvijay]
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
What is OOP?
What is OOP?What is OOP?
What is OOP?
 
Oops
OopsOops
Oops
 
Concept of Object Oriented Programming
Concept of Object Oriented Programming Concept of Object Oriented Programming
Concept of Object Oriented Programming
 
Object Oriented Software Development revision slide
Object Oriented Software Development revision slide Object Oriented Software Development revision slide
Object Oriented Software Development revision slide
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
Oops
OopsOops
Oops
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
Advanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, IdiomsAdvanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, Idioms
 
Encapsulation of operations, methods & persistence
Encapsulation of operations, methods & persistenceEncapsulation of operations, methods & persistence
Encapsulation of operations, methods & persistence
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 

Viewers also liked

Viewers also liked (9)

Risk08a
Risk08aRisk08a
Risk08a
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1
 
Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)
 
Npv rule
Npv ruleNpv rule
Npv rule
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npv
 
Mcq sample
Mcq sampleMcq sample
Mcq sample
 
Vörösiszap és timföld
Vörösiszap és timföldVörösiszap és timföld
Vörösiszap és timföld
 
Npv2214(1)
Npv2214(1)Npv2214(1)
Npv2214(1)
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firm
 

Similar to Is2215 lecture3 student (1)

Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsMaryo Manjaruni
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01Zafor Iqbal
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 

Similar to Is2215 lecture3 student (1) (20)

Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Classes2
Classes2Classes2
Classes2
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Inheritance
InheritanceInheritance
Inheritance
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Inheritance
InheritanceInheritance
Inheritance
 
Core java oop
Core java oopCore java oop
Core java oop
 
Inheritance
InheritanceInheritance
Inheritance
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
inheritance
inheritanceinheritance
inheritance
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 

More from dannygriff1

Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)dannygriff1
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)dannygriff1
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)dannygriff1
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)dannygriff1
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopolydannygriff1
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysisdannygriff1
 
4 production and cost
4  production and cost4  production and cost
4 production and costdannygriff1
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choicedannygriff1
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticitydannygriff1
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)dannygriff1
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introdannygriff1
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessdannygriff1
 
Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesdannygriff1
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)dannygriff1
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)dannygriff1
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesdannygriff1
 

More from dannygriff1 (18)

Npvrisk
NpvriskNpvrisk
Npvrisk
 
Irr(1)
Irr(1)Irr(1)
Irr(1)
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopoly
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysis
 
4 production and cost
4  production and cost4  production and cost
4 production and cost
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choice
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticity
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_intro
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_access
 
Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibraries
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
 
Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
 

Is2215 lecture3 student (1)

  • 1. FROM THEORY TO PRACTICE EXTENDING OUR CLASS
  • 2. Inheritance Base Class Student Graduate Derived Class
  • 3. Create Properties Inherits Keyword Private Property Property Procedure
  • 7.
  • 8. Class Person Public Name As String Public Address As String Public City As String End Class
  • 9. Public Class Customer Public Name As String Public Address As String Public City As String End Class Public Class Employee Public Name As String Public Addresss As String Public City As String End Class
  • 10.
  • 11. VB.NET Supports Single Inheritance Public Class Programmer Inherits Employee Public Project As String End Class Public Class Manager Inherits Employee Public intMangedEmployees As Integer End Class
  • 12.
  • 13. 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
  • 14. 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
  • 15. Con’td Class Student Private strStudentID As String Protected CourseTitle As String End Class
  • 16. Cont’d Class Guest Inherits User Sub New ( ) ‘Results in an Error message ‘Error:strStudentID is private to Student strStudentID= “90000000” ‘This will work CourseTitle = “BIS” End Sub
  • 18. 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
  • 19. An Example Public Class ParentClass Public Overridable Sub ParentMethod() MsgBox(“Hello World from parent”) End Sub End Class Public Class ChildClass Inherits ParentClass Public Overrides Sub ParentMethod() msgBox(“Hello World from child”) End Sub End Class
  • 21. 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.
  • 22. 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)
  • 23. Overloading Methods Cont’d Public Function SearchEmployees(ByRef Name _ As String) As Boolean „Code to search through the database MessageBox.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:
  • 24. 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 Database MessageBox.Show (“Employee Found”) End Function
  • 25. 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
  • 26. 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 explicitly System.GC.Collect()
  • 27. 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 here End Sub MyStudent = Nothing