Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz."— Presentation transcript:

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

2 Compunet Corporation2 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

3 Compunet Corporation3 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 ClassName2 Inherits ClassName1 // body of the class End Class

4 Compunet Corporation4 Inheritance Method Example Imports System.Console Class Class1 Sub Method1() WriteLine("Base class method1") End Sub End Class Class Class2 Inherits Class1 ' On next Line Sub Method2() WriteLine("Sub class method2") End Sub End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() C1.Method1() C2.Method1() C2.Method2() End Sub End Class Output: Base class method1 Sub class method2

5 Compunet Corporation5 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.

6 Compunet Corporation6 Inheritance Public Variable Example Imports System.Console Class Class1 Public Dim J As Integer=100 End Class Class Class2 Inherits Class1 ' On next Line Public Dim K As Integer=300 End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() WriteLine(C1.J) ' 100 WriteLine(C2.J) ' 100 WriteLine(C2.K) ' 300 End Sub End Class Output: 100 300

7 Compunet Corporation7 Inheritance Private Variable Example Imports System.Console Class Class1 Dim J As Integer=100 End Class Class Class2 Inherits Class1 ' On next Line Dim K As Integer=300 End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() WriteLine(C1.J) ' Error WriteLine(C2.J) ' Error WriteLine(C2.K) ' Error End Sub End Class Output: Compilation Error, because J and K are private and are Not accessible outside the class

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

9 Compunet Corporation9 Inheritance Example Imports System.Console Class Class1 Protected Dim J As Integer=100 End Class Class Class2 Inherits Class1 ' On next Line Dim K As Integer=300 Sub Method1() WriteLine (J) WriteLine (K) End Sub End Class Class M1 Shared Sub Main() Dim C2 As New class2() C2.Method1() End Sub End Class Output: 100 300

10 Compunet Corporation10 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.

11 Compunet Corporation11 Inheritance Shadows Example Imports System.Console Public Class BaseCls Public Z As Integer = 100 End Class Public Class DervCls Inherits BaseCls Public Shadows Z As String = "*" End Class Public Class UseClasses shared Sub Main() Dim BObj As BaseCls = New DervCls() Dim DObj As DervCls = New DervCls() WriteLine("Accessed through base class: " & BObj.Z) WriteLine("Accessed through derived class: " & DObj.Z) End Sub End Class

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

13 Compunet Corporation13 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.

14 Compunet Corporation14 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)

15 Compunet Corporation15 Example: Method Overriding Imports System.Console Class Class1 Sub Method1() WriteLine("Base class method1") End Sub End Class Class Class2 Inherits Class1 ' On next Line Shadows Sub Method1() WriteLine("Sub class method1") End Sub Sub Method2() WriteLine("Sub class method2") End Sub End Class Class M1 Shared Sub Main() Dim C1 As New class1() Dim C2 As New class2() C1.Method1() C2.Method1() C2.Method2() End Sub End Class Output: Base class method1 Sub class method1 Sub class method2

16 Compunet Corporation16 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 MustInherit Abstract methods that are to be implemented are marked in Visual Basic with the MustOverride modifier

17 Compunet Corporation17 Abstract Classes Public MustInherit Class WashingMachine Sub New() ' Code to instantiate the class goes here. End sub Public MustOverride Sub Wash Public MustOverride Sub Rinse (loadSize as Integer) Public MustOverride Function Spin (speed as Integer) as Long End Class

18 Compunet Corporation18 Abstract Classes Public Class MyWashingMachine Inherits WashingMachine Public Overrides Sub Wash() ' Wash code goes here End Sub Public Overrides Sub Rinse (loadSize 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


Download ppt "Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz."

Similar presentations


Ads by Google