Programming with Visual Basic NET Inheritance Lecture 10

Programming with Visual Basic. NET Inheritance Lecture # 10 Tariq Ibn Aziz Compunet Corporation 1

Inheritance • Inheritance – The original class is called the base class – The new class is often called the derived class or the subclass – Inheritance is often referred to as extending the base class Compunet Corporation 2

Inheritance • One of the primary objective of OOP, the reuse the code can be achieved by this technique • The Inherits clause in a class declaration establishes an inheritance relationship between two classes class Class. Name 2 Inherits Class. Name 1 // body of the class End Class Compunet Corporation 3

Inheritance Method Example Imports System. Console Class 1 Sub Method 1() Write. Line("Base class method 1") End Sub End Class 2 Inherits Class 1 ' On next Line Sub Method 2() Write. Line("Sub class method 2") End Sub End Class M 1 Shared Sub Main() Dim C 1 As New class 1() Dim C 2 As New class 2() C 1. Method 1() C 2. Method 2() End Sub End Class Output: Base class method 1 Sub class method 2 Compunet Corporation 4

Inheritance and Variables Inherit Variable • A class inherits the state(variable) and behavior (method) defined by all of its super classes. Therefore an object has one copy of every instance variable of its own class and its super classes. Compunet Corporation 5

Inheritance Public Variable Example Imports System. Console Class 1 Public Dim J As Integer=100 End Class 2 Inherits Class 1 ' On next Line Public Dim K As Integer=300 End Class M 1 Shared Sub Main() Dim C 1 As New class 1() Dim C 2 As New class 2() Write. Line(C 1. J) ' 100 Write. Line(C 2. K) ' 300 End Sub End Class Output: 100 300 Compunet Corporation 6

Inheritance Private Variable Example Imports System. Console Class 1 Dim J As Integer=100 End Class 2 Inherits Class 1 ' On next Line Dim K As Integer=300 End Class M 1 Shared Sub Main() Dim C 1 As New class 1() Dim C 2 As New class 2() Write. Line(C 1. J) ' Error Write. Line(C 2. K) ' Error End Sub End Class Output: Compilation Error, because J and K are private and are Not accessible outside the class Compunet Corporation 7

Inheritance Protected Variables • Protected elements are accessible only from within their own class or from a derived class. Compunet Corporation 8

Inheritance Example Imports System. Console Class 1 Protected Dim J As Integer=100 End Class 2 Inherits Class 1 ' On next Line Dim K As Integer=300 Sub Method 1() Write. Line (J) Write. Line (K) End Sub End Class M 1 Shared Sub Main() Dim C 2 As New class 2() C 2. Method 1() End Sub End Class Output: 100 300 Compunet Corporation 9

Inheritance and Variables Inherit Variable Hiding • If a variable of a class has a same name (type maybe different) as a base class variable, in that case derived class variable hides the base class variable. Compunet Corporation 10

Inheritance Shadows Example Imports System. Console Public Class Base. Cls Public Z As Integer = 100 End Class Public Class Derv. Cls Inherits Base. Cls Public Shadows Z As String = "*" End Class Public Class Use. Classes shared Sub Main() Dim BObj As Base. Cls = New Derv. Cls() Dim DObj As Derv. Cls = New Derv. Cls() Write. Line("Accessed through base class: " & BObj. Z) Write. Line("Accessed through derived class: " & DObj. Z) End Sub End Class Compunet Corporation 11

Inheritance Shadows Example • Output: Accessed through base class: 100 Accessed through derived class: * Compunet Corporation 12

Exercise (Inheritance and Variables) • Write an application that demonstrates a class inheritance hierarchy. Class M extends Object and has two instance variable of type float and String. Class N extends M and has one instance variable of type Double. Instantiate class N. Initialize and display its variables. • Write an application that illustrates how to access a hidden variable. Class G declares a static variable x. Class H extends G and declares an instance variable x. A display() method in H displays both of these variables. Compunet Corporation 13

Method Overriding • Method overriding occurs when a class declares a method that has the same type signature as a method declared by one of its Base classes • When a method in a subclass override a method in a Base class, the method in the Base class is hidden relative to the subclass object. In Derived class you need to Shadows the method • Method overriding is a very important capability because it forms the basis for run-time polymorphism(means one interface, multiple implementation) Compunet Corporation 14

Example: Method Overriding Imports System. Console Class 1 Sub Method 1() Write. Line("Base class method 1") End Sub End Class 2 Inherits Class 1 ' On next Line Shadows Sub Method 1() Write. Line("Sub class method 1") End Sub Method 2() Write. Line("Sub class method 2") End Sub End Class M 1 Shared Sub Main() Dim C 1 As New class 1() Dim C 2 As New class 2() C 1. Method 1() C 2. Method 2() End Sub End Class Output: Base class method 1 Sub class method 2 Compunet Corporation 15

Abstract Classes • Abstract classes cannot be instantiated. • A class that is derived from an abstract class may still implement interfaces. • An abstract class is denoted in Visual Basic by the keyword Must. Inherit • Abstract methods that are to be implemented are marked in Visual Basic with the Must. Override modifier Compunet Corporation 16

Abstract Classes Public Must. Inherit Class Washing. Machine Sub New() ' Code to instantiate the class goes here. End sub Public Must. Override Sub Wash Public Must. Override Sub Rinse (load. Size as Integer) Public Must. Override Function Spin (speed as Integer) as Long End Class Compunet Corporation 17

Abstract Classes Public Class My. Washing. Machine Inherits Washing. Machine Public Overrides Sub Wash() ' Wash code goes here End Sub Public Overrides Sub Rinse (load. Size as Integer) ' Rinse code goes here End Sub Public Overrides Function Spin (speed as Integer) as Long ' Spin code goes here End Sub End Class Compunet Corporation 18
- Slides: 18