You are on page 1of 6

LAB 5 – CLASS AND INHERITANCE

OBJECTIVE:

The main objective is to introduce the students with the class, derived class and inheritance concept in
OOP.

INTRODUCTION:

In object-oriented programming, inheritance is a way to form new classes (instances of which are called
objects) using classes that have already been defined. The new classes, known as derived classes, take over (or
inherit) attribute and behavior of the pre-existing classes, which are referred to as base classes (or ancestor
classes). It is intended to help reuse existing code with little or no modification.

Inheritance provides the support for representation by Categorization in computer languages. Categorization is
a powerful mechanism number of information processing, crucial to human learning by means of generalization
(what is known about specific entities is applied to a wider group given a belongs relation can be established)
and cognitive economy (less information needs to be stored about each specific entity, only its particularities).

Inheritance is typically accomplished either by overriding (replacing) one or more methods exposed by ancestor,
or by adding new methods to those exposed by an ancestor.

Complex inheritance, or inheritance used within a design that is not sufficiently mature, may lead to the Yo-yo
problem.

Structural Inheritance

– Base Class

• Base-class must be declared as overridable if that method is to be overridden in the derived class

• The base-class should be able to change its implementation freely

– Derived Class

• Derived-class object can assign an illegal value to the Protected data, hence leaving the object in an
inconsistent state

• Derived-classes should depend only on the base-classes services (non private methods and
properties)

The following code demonstrates the implementation of inheritance. Code the program in VB.Net and try to
understand the program.

INSTRUCTIONS:

Instruction : Open VB 2008  Create Project  Windows Form Application  Time.vbproj

1. From menu project  add class  Name your class as CTime.vb

Class CTime
Inherits Object

Session: Jan - June 2008

Subject Code: FSB23103 Page: 1 of 6


LAB 5 – CLASS AND INHERITANCE

' declare Integer instance values for hour, minute and second
Private mHour As Integer ' 0 - 23
Private mMinute As Integer ' 0 - 59
Private mSecond As Integer ' 0 - 59

' Method New is the CTime constructor method, which initializes


' instance variables to zero
Public Sub New()
SetTime(0, 0, 0)
End Sub ' New

' set new time value using universal time;


' perform validity checks on data;
' set invalid values to zero
Public Sub SetTime(ByVal hourValue As Integer, _
ByVal minuteValue As Integer, ByVal secondValue As Integer)

' check if hour is between 0 and 23, then set hour


If (hourValue >= 0 AndAlso hourValue < 24) Then
mHour = hourValue
Else
mHour = 0
End If
' check if minute is between 0 and 59, then set minute
If (minuteValue >= 0 AndAlso minuteValue < 60) Then
mMinute = minuteValue
Else
mMinute = 0
End If

' check if second is between 0 and 59, then set second


If (secondValue >= 0 AndAlso secondValue < 60) Then
mSecond = secondValue
Else
mSecond = 0
End If

End Sub ' SetTime

' convert String to universal-time format


Public Function ToUniversalString() As String
Return String.Format("{0}:{1:D2}:{2:D2}", _
mHour, mMinute, mSecond)
End Function ' ToUniversalString

' convert to String in standard-time format


Public Function ToStandardString() As String
Dim suffix As String = " PM"
Dim format As String = "{0}:{1:D2}:{2:D2}"
Dim standardHour As Integer

Session: Jan - June 2008

Subject Code: FSB23103 Page: 2 of 6


LAB 5 – CLASS AND INHERITANCE

' determine whether time is AM or PM


If mHour < 12 Then
suffix = " AM"
End If
' convert from universal-time format to standard-time format
If (mHour = 12 OrElse mHour = 0) Then
standardHour = 12
Else
standardHour = mHour Mod 12
End If

Return String.Format(format, standardHour, mMinute, _


mSecond) & suffix
End Function ' ToStandardString

End Class ' CTime

2. Goto menu project  Add Module  Name your module  modTestTime.vb

' Fig. 8.2: TimeTest.vb


' Demonstrating class CTime.

Imports System.Windows.Forms

Module modTimeTest

Sub Main()
Dim time As New CTime() ' call CTime constructor
Dim output As String

output = "The initial universal times is: " & _


time.ToUniversalString() & vbCrLf & _
"The initial standard time is: " & _
time.ToStandardString()

time.SetTime(13, 27, 6) ' set time with valid settings

output &= vbCrLf & vbCrLf & _


"Universal time after setTime is: " & _
time.ToUniversalString() & vbCrLf & _
"Standard time after setTime is: " & _
time.ToStandardString()

time.SetTime(99, 99, 99) ' set time with invalid settings

output &= vbCrLf & vbCrLf & _


"After attempting invalid settings: " & vbCrLf & _

Session: Jan - June 2008

Subject Code: FSB23103 Page: 3 of 6


LAB 5 – CLASS AND INHERITANCE

"Universal time: " & time.ToUniversalString() & _


vbCrLf & "Standard time: " & time.ToStandardString()

MessageBox.Show(output, "Testing Class CTime")


End Sub ' Main

End Module ' modTimeTest


Create the class name CPoint in Vb.Net

1' Fig. 9.4: Point.vb


2 ' CPoint class represents an x-y coordinate pair.
3
4 Public Class CPoint
5 ' implicitly Inherits Object
6
7 ' point coordinate
8 Private mX, mY As Integer
9
10 ' default constructor
11 Public Sub New()
12
13 ' implicit call to Object constructor occurs here
14 X = 0
15 Y = 0
16 End Sub ' New
17
18 ' constructor
19 Public Sub New(ByVal xValue As Integer, _
20 ByVal yValue As Integer)
21
22 ' implicit call to Object constructor occurs here
23 X = xValue
24 Y = yValue
25 End Sub ' New
26
27 ' property X
28 Public Property X() As Integer
29
30 Get
31 Return mX
32 End Get
33
34 Set(ByVal xValue As Integer)
35 mX = xValue ' no need for validation
36 End Set
37
38 End Property ' X
39
40 ' property Y
41 Public Property Y() As Integer
42
43 Get
44 Return mY
45 End Get
46
47 Set(ByVal yValue As Integer)
48 mY = yValue ' no need for validation
49 End Set
50

Session: Jan - June 2008

Subject Code: FSB23103 Page: 4 of 6


LAB 5 – CLASS AND INHERITANCE

51 End Property ' Y


52
53 ' return String representation of CPoint
54 Public Overrides Function ToString() As String
55 Return "[" & mX & ", " & mY & "]"
56 End Function ' ToString
57
58 End Class ' CPoint

Create the module below to test Class CPoint


1 ' Fig. 9.5: PointTest.vb
2 ' Testing class CPoint.
3
4 Imports System.Windows.Forms
5
6 Module modPointTest
7
8 Sub Main()
9 Dim point As CPoint
10 Dim output As String
11
12 point = New CPoint(72, 115) ' instantiate CPoint object
13
14 ' display point coordinates via X and Y properties
15 output = "X coordinate is " & point.X & _
16 vbCrLf & "Y coordinate is " & point.Y
17
18 point.X = 10 ' set x-coordinate via X property
19 point.Y = 10 ' set y-coordinate via Y property
20
21 ' display new point value
22 output &= vbCrLf & vbCrLf & _
23 "The new location of point is " & point.ToString()
24
25 MessageBox.Show(output, "Demonstrating Class Point")
26 End Sub ' Main
27
28 End Module ' modPointTest

Create another class named CCircle to inherit the Class CPoint as below
code. Observe which part of in class CCircle inherit the class CPoint.
1 ' Fig. 9.8: Circle2.vb
2 ' CCircle2 class that inherits from class CPoint.
3
4 Public Class CCircle2
5 Inherits CPoint ' CCircle2 Inherits from class CPoint
6
7 Private mRadius As Double ' CCircle2's radius
8
9 ' default constructor
10 Public Sub New()
11
12 ' implicit call to CPoint constructor occurs here
13 Radius = 0
14 End Sub ' New
15
16 ' constructor
17 Public Sub New(ByVal xValue As Integer, _
18 ByVal yValue As Integer, ByVal radiusValue As Double)

Session: Jan - June 2008

Subject Code: FSB23103 Page: 5 of 6


LAB 5 – CLASS AND INHERITANCE

19
20 ' implicit call to CPoint constructor occurs here
21 mX = xValue
22 mY = yValue
23 Radius = radiusValue
24 End Sub ' New
25
26 ' property Radius
27 Public Property Radius() As Double
28
29 Get
30 Return mRadius
31 End Get
32
33 Set(ByVal radiusValue As Double)
34
35 If radiusValue > 0
36 mRadius = radiusValue
37 End If
38
39 End Set
40
41 End Property ' Radius
42
43 ' calculate CCircle2 diameter
44 Public Function Diameter() As Double
45 Return mRadius * 2
46 End Function ' Diameter
47
48 ' calculate CCircle2 circumference
49 Public Function Circumference() As Double
50 Return Math.PI * Diameter()
51 End Function ' Circumference
52
53 ' calculate CCircle2 area
54 Public Function Area() As Double
55 Return Math.PI * mRadius ^ 2
56 End Function ' Area
57
58 ' return String representation of CCircle2
59 Public Overrides Function ToString() As String
60 Return "Center = " & "[" & mX & ", " & mY & "]" & _
61 "; Radius = " & mRadius
62 End Function ' ToString
63
64 End Class ' CCircle2

Session: Jan - June 2008

Subject Code: FSB23103 Page: 6 of 6