|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Why Use Inheritance
| When you want to define a new class based on another existing class, and you want to inherit or reuse all members of the existing class into your new class, inheritance could be used. All members of the base/parent class are inherited/added to the derived/child class. |
| Class Person
Private m_Name As String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
End Class
Class Student : Inherits Person
Private m_ClassGroup As String
Public Property ClassGroup() As String
Get
ClassGroup = m_ClassGroup
End Get
Set(ByVal value As String)
m_ClassGroup = ClassGroup
End Set
End Property
End Class Download Broncode |
| The Student class inherits from the Person class, and inserts all members of Person. In the following client you can see how the object student1 can use the public member Name defined in the Student, but also the public member ClassGroup inherited from Person. |
| Module Client1
Sub Main()
Dim person1 As Person = New Person
person1.Name = "John"
Console.WriteLine(person1.Name)
Dim student1 As Student = New Student
student1.Name = "Jane"
Console.WriteLine(student1.Name)
student1.ClassGroup = "Group 1"
Console.WriteLine(student1.ClassGroup)
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : John
Jane
Group 1 |
Up
How to Define Inheritance
| Inheritance can be defined by adding an Inherits clause to definition of the derived class. Or by adding the clause after the identifier of the inherited class ( after a colon ), or by adding the Inherits clause on the first line of the definition of the derived class. |
| Class Teacher
Inherits Person
Private m_Course As String
Public Property Course() As String
Get
Course = m_Course
End Get
Set(ByVal value As String)
m_Course = value
End Set
End Property
End Class Download Broncode |
Up
Single Inheritance
| As you can see multiple classes can be derived from a single base class. In Visual Basic .NET you can only inherit from one class ( "single inheritance" ), not from multiple classes ( "multiple inheritance" ). |
Up
Single And Static Classification
| In Visual Basic .NET an object is always an instance of one specific class ( "single classification" ). The type of the object can never change ( "static classification" ). Some other languages can created objects of multiple classes ( "multiple classification" ) or change the type of the object ( "dynamic classification" ). |
Up
When to Use Inheritance
| When inheritance is suitable, there usually is an "is a"-relation between the objects of both entities. In this example you can say that a Student or a Teacher is a Person. Don't confuse this with other types of associations, for instance a "HAS-A"-relationship. Other examples of inheritance could be : a home is a building, a car is a vehicle, a manager is an employee, a text processor is a program, ... . Don't confuse instances with subtypes. John is a person. But John is probably rather an instance of 'Person' than a subtype of 'Person'.
A derived class can also be a base class for other derived classes. Actually every class you can define in .NET is a derived class. Even if you don't explicitly define inheritance ( with an 'Inherits'-clause ), the default 'Inherits System.Object' is used. So class Person is derived from System.Object. Further on more details about System.Object.
Inheritance is a technique to reuse code. All members are inherited and can be used on the objects of the derived class.
Do not misuse inheritance simple to avoid redefining the members in the derived class. The power of inheritance is rather dynamic polymorphism than reuse of code. So the derived class should always be a specialization of the derived class. Making it possible to threat objects of the derived class in the form of the base class.
Stick to the following to guidelines when deciding whether or not to use inheritance : - the "is a" guideline - the "100%" guideline : all members of the base class should be useful for the derived class Only apply inheritance when both guidelines are appropriate.
When you notice that multiple classes define the same members, you can decide to define a general base type ( with the common members ) to inherit from ( "generalization" ).
When you notice that a new class is a specialization of some other class, you can decide to inherits from that other class ( "specialization" ).
The result is the same, that is inheritance.
For the moment its good to know that when you inherit a members with a specific signature from the base class, you cant add an other member with an identical signature to the derived class. At least, no without using redefinition or shadowing. Later more on those topics. |
Up
An Other Example
| We start from a class 'Counter', objects of this type can be queried for their value ( 'GetValue' ). It's also possible to raise ( 'Raise' ) or lower ( 'Lower' ) the value of a 'Counter' object. |
| Class Counter
Protected m_Value As Integer
Public Function GetValue() As Integer
GetValue = m_Value
End Function
Public Sub Raise()
m_Value += 1
End Sub
Public Sub Lower()
m_Value -= 1
End Sub
End Class Download Broncode |
| Afterwards we wish a kind of Counter, one on which we want to be able to set the value to a specific amount.
We could decide to create a specialized type ( something like SettableCounter ). We could also edit the existing class Counter to fit our needs, but in this case we want to leave the existing Counter untouched.
All kinds of reasons could be thought of to keep Counter untouched. Maybe you simply want to avoid risking to corrupt the existing class. Maybe you want to keep its abstraction, an value-settable and a non-settable-value counter is a different thing. Maybe youre not able to change the code of "sourcecode">Counter because its compiled, and you dont have access to the sourcecode. |
| Class SettableCounter : Inherits Counter
Public Sub SetValue(ByVal value As Integer)
m_Value = value
End Sub
End Class
Module Client2
Sub Main()
Dim counter1 As Counter = New Counter
With counter1
Console.WriteLine(.GetValue() = 0)
.Raise()
Console.WriteLine(.GetValue() = 1)
.Lower()
Console.WriteLine(.GetValue() = 0)
End With
Dim specialCounter1 As SettableCounter = New SettableCounter
With specialCounter1
Console.WriteLine(.GetValue() = 0)
.SetValue(10)
Console.WriteLine(.GetValue() = 10)
.Raise()
Console.WriteLine(.GetValue() = 11)
.Lower()
Console.WriteLine(.GetValue() = 10)
End With
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : True
True
True
True
True
True
True |
Up
Access Modifier Protected
| Private members are only available within the class in which they are defined. Public members are available to all clients who have access to the class in which they are defined.
New are the Protected members. These are encapsulated within the class, but are also available for the derived class. Other client ( not derived from that class ) have no access to these members.
The field 'm_Value' of 'Counter' can be encapsulated, it doesn't have to be available for clients. But the derived class 'SettableCounter' needs access to this member for its 'SetValue' implementation.
Every time you want to encapsulate a member, you have to decide whether or not it must be accessible for eventual ( future ) derived classes. This is not easy, often you won't have a clue what possible derived class will be made.
Remember that all members of a base class are inherited into its derived classes, also the encapsulated members. |
Up
Exercises
| Task 1 :
Define the necessary classes from which we can create objects that represent an employee. An employee has a name and an address ( street, number, zip code and city ).
Also we want to create objects that represent a manager. A manager is an employee. An employee has a name, an address and a car ( of some brand ). |
| Class Address
Private m_Street As String
Public Property Street() As String
Get
Street = m_Street
End Get
Set(ByVal value As String)
m_Street = value
End Set
End Property
Private m_Number As String
Public Property Number() As String
Get
Number = m_Number
End Get
Set(ByVal value As String)
m_Number = value
End Set
End Property
Private m_ZipCode As String
Public Property ZipCode() As String
Get
ZipCode = m_ZipCode
End Get
Set(ByVal value As String)
m_ZipCode = value
End Set
End Property
Private m_City As String
Public Property City() As String
Get
City = m_City
End Get
Set(ByVal value As String)
m_City = value
End Set
End Property
End Class
Class Employee
Private m_Name As String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Address As Address
Public Property Address() As Address
Get
Address = m_Address
End Get
Set(ByVal value As Address)
m_Address = value
End Set
End Property
End Class
Class Manager : Inherits Employee
Private m_Car As Car
Public Property Car() As Car
Get
Car = m_Car
End Get
Set(ByVal value As Car)
m_Car = value
End Set
End Property
End Class
Class Car
Private m_Brand As String
Public Property Brand() As String
Get
Brand = m_Brand
End Get
Set(ByVal value As String)
m_Brand = value
End Set
End Property
End Class Download Broncode |
| A list of names and nouns :
Sarkozy, monarch, Paris, country, United Kingdom, president, monarchy, France, capital, London, Elizabeth, republic
Use all above words in a program. Chose for every word if it suites best as a class-identifier, a member-identifier, a value for a property or an object-identifier. |
| Module Exercise2Solution
Sub Main()
Dim france As Republic = New Republic
france.Capital = "Paris"
france.President = "Sarkozy"
Dim unitedKingdom As Monarchy = New Monarchy
unitedKingdom.Capital = "London"
unitedKingdom.Monarch = "Elizabeth"
End Sub
End Module
Class Country
Private m_Capital As String
Public Property Capital() As String
Get
Capital = m_Capital
End Get
Set(ByVal value As String)
m_Capital = value
End Set
End Property
End Class
Class Monarchy : Inherits Country
Private m_Monarch As String
Public Property Monarch() As String
Get
Monarch = m_Monarch
End Get
Set(ByVal value As String)
m_Monarch = value
End Set
End Property
End Class
Class Republic : Inherits Country
Private m_President As String
Public Property President() As String
Get
President = m_President
End Get
Set(ByVal value As String)
m_President = value
End Set
End Property
End Class Download Broncode |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|