240 likes | 476 Views
Programming with Visual Basic .NET. Inheritance Lecture # 10 Tariq Ibn Aziz. 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. Inheritance.
E N D
Programming with Visual Basic .NET Inheritance Lecture # 10 Tariq Ibn Aziz Compunet Corporation
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
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 classClassName2 Inherits ClassName1 // body of the class End Class Compunet Corporation
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 Inheritance Method Example Output: Base class method1 Base class method1 Sub class method2 Compunet Corporation
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
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 Inheritance Public Variable Example Output: 100 100 300 Compunet Corporation
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 Inheritance Private Variable Example Output: Compilation Error, because J and K are private and are Not accessible outside the class Compunet Corporation
Inheritance Protected Variables • Protected elements are accessible only from within their own class or from a derived class. Compunet Corporation
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 Inheritance Example Output: 100 300 Compunet Corporation
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
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 Compunet Corporation
Inheritance Shadows Example • Output: Accessed through base class: 100 Accessed through derived class: * Compunet Corporation
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
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
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 Example: Method Overriding Output: Base class method1 Sub class method1 Sub class method2 Compunet Corporation
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 Compunet Corporation
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 Compunet Corporation
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 Compunet Corporation