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 52 53 54 55 56 57 58
End Property ' Y ' return String representation of CPoint Public Overrides Function ToString() As String Return "[" & mX & ", " & mY & "]" End Function ' ToString End Class ' CPoint
Create the module below to test Class CPoint 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
' Fig. 9.5: PointTest.vb ' Testing class CPoint. Imports System.Windows.Forms Module modPointTest Sub Main() Dim point As CPoint Dim output As String point = New CPoint(72, 115) ' instantiate CPoint object ' display point coordinates via X and Y properties output = "X coordinate is " & point.X & _ vbCrLf & "Y coordinate is " & point.Y point.X = 10 ' set x-coordinate via X property point.Y = 10 ' set y-coordinate via Y property ' display new point value output &= vbCrLf & vbCrLf & _ "The new location of point is " & point.ToString() MessageBox.Show(output, "Demonstrating Class Point") End Sub ' Main 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
' Fig. 9.8: Circle2.vb ' CCircle2 class that inherits from class CPoint. Public Class CCircle2 Inherits CPoint ' CCircle2 Inherits from class CPoint Private mRadius As Double ' CCircle2's radius ' default constructor Public Sub New() ' implicit call to CPoint constructor occurs here Radius = 0 End Sub ' New ' constructor Public Sub New(ByVal xValue As Integer, _ 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
' implicit call to CPoint constructor occurs here mX = xValue mY = yValue Radius = radiusValue End Sub ' New ' property Radius Public Property Radius() As Double Get Return mRadius End Get Set(ByVal radiusValue As Double) If radiusValue > 0 mRadius = radiusValue End If End Set End Property ' Radius ' calculate CCircle2 diameter Public Function Diameter() As Double Return mRadius * 2 End Function ' Diameter ' calculate CCircle2 circumference Public Function Circumference() As Double Return Math.PI * Diameter() End Function ' Circumference ' calculate CCircle2 area Public Function Area() As Double Return Math.PI * mRadius ^ 2 End Function ' Area ' return String representation of CCircle2 Public Overrides Function ToString() As String Return "Center = " & "[" & mX & ", " & mY & "]" & _ "; Radius = " & mRadius End Function ' ToString End Class ' CCircle2
Session: Jan - June 2008 Subject Code: FSB23103
Page: 6 of 6