 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship.
C++ Inheritance Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
 2002 Prentice Hall. All rights reserved. Some slides modified by LL 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance Many slides modified by Prof. L. Lilien (even many without.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Chapter 12: Adding Functionality to Your Classes.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Programming Pillars Introduction to Object- Oriented Programming.
1 Advanced Programming Lecture 5 Inheritance. 2 static Class Members Every object of a class has its own copy of all instance variables Sometimes it is.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
 2002 Prentice Hall. All rights reserved. 1 Introduction Polymorphism allows programmers to write: –Programs that handle a wide variety of related classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
 2002 Prentice Hall. All rights reserved. 1 Week 4: OOP Questions from last lecture: –Single-subscript arrays –Multiple-subscript arrays –Homework/Practical.
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Object-Oriented Programming: Part 2 Outline 10.1Introduction 10.2 Derived-Class-Object to Base-Class-Object.
Jozef Goetz contribution,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Jozef Goetz contribution,  2011, 2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Jozef Goetz contribution, Credits go to  2014, 2011, 2009 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
These materials where developed by Martin Schray
Chapters 9, 10 – Object-Oriented Programming: Inheritance
Object-Oriented Programming: Classes and Objects
Chapter 9 – Object-Oriented Programming: Inheritance
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived Classes 9.3 Protected and Friend Members 9.4 Relationship between Base Classes and Derived Classes 9.5 Constructors and Finalizers in Derived Classes 9.6 Implicit Derived-Class-Object to Base-Class-Object Conversion 9.7 Software Engineering with Inheritance 9.8 Composition vs. Inheritance 9.9 Case Study: Point, Circle, Cylinder 9.10 Visual Inheritance

 2002 Prentice Hall. All rights reserved Introduction Fig. 9.1Some simple inheritance examples.

 2002 Prentice Hall. All rights reserved Base Classes and Derived Classes Fig. 9.2Inheritance hierarchy for university CCommunityMembers. CommunityMemeber EmployeeStudentAlumnus FacultyStaff AdministratorTeacher

 2002 Prentice Hall. All rights reserved Protected and Friend Members Fig. 9.3Portion of a CShape class hierarchy. CShape CTwoDimensionalShapeCThreeDimensionalShape CSphereCCubeCCylinderCTriangleCSquareCCircle

 2002 Prentice Hall. All rights reserved Relationship Between Base Classes and Derived Classes 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)

 2002 Prentice Hall. All rights reserved. Outline 6 CPoint.vb 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() ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 End Sub ' New ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 End Sub ' New ' property X 28 Public Property X() As Integer Get 31 Return mX 32 End Get 33 Two CPoint Constructors Property of X Declares integer variables mX and mY as Private

 2002 Prentice Hall. All rights reserved. Outline 7 CPoint.vb 34 Set(ByVal xValue As Integer) 35 mX = xValue ' no need for validation 36 End Set End Property ' X ' property Y 41 Public Property Y() As Integer Get 44 Return mY 45 End Get Set(ByVal yValue As Integer) 48 mY = yValue ' no need for validation 49 End Set End Property ' Y ' return String representation of CPoint 54 Public Overrides Function ToString() As String 55 Return "[" & mX & ", " & mY & "]" 56 End Function ' ToString End Class ' CPoint Method ToString contains keyword Overrides Property of Y

 2002 Prentice Hall. All rights reserved. Outline 8 PointTest.vb 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 point = New CPoint(72, 115) ' instantiate CPoint object ' display point coordinates via X and Y properties 15 output = "X coordinate is " & point.X & _ 16 vbCrLf & "Y coordinate is " & point.Y point.X = 10 ' set x-coordinate via X property 19 point.Y = 10 ' set y-coordinate via Y property ' display new point value 22 output &= vbCrLf & vbCrLf & _ 23 "The new location of point is " & point.ToString() MessageBox.Show(output, "Demonstrating Class Point") 26 End Sub ' Main End Module ' modPointTest Instantiates an object of class CPoint Sets X and Y coordinates Retrieves values X and Y and appends values to String Output Changes values of X and Y Outputs String representation of X and Y

 2002 Prentice Hall. All rights reserved. Outline 9 Circle.vb 1 ' Fig. 9.6: Circle.vb 2 ' CCircle class contains x-y coordinate pair and radius. 3 4 Public Class CCircle 5 6 ' coordinate of center of CCircle 7 Private mX, mY As Integer 8 Private mRadius As Double ' CCircle's radius 9 10 ' default constructor 11 Public Sub New() ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 Radius = 0 17 End Sub ' New ' constructor 20 Public Sub New(ByVal xValue As Integer, _ 21 ByVal yValue As Integer, ByVal radiusValue As Double) ' implicit call to Object constructor occurs here 24 X = xValue 25 Y = yValue 26 Radius = radiusValue 27 End Sub ' New ' property X 30 Public Property X() As Integer Get 33 Return mX 34 End Get 35 Two CCircle constructors of X, Y coordinates and Radius Property of X Declared integer variables mX and mY as Private

 2002 Prentice Hall. All rights reserved. Outline 10 Circle.vb 36 Set(ByVal xValue As Integer) 37 mX = xValue ' no need for validation 38 End Set End Property ' X ' property Y 43 Public Property Y() As Integer Get 46 Return mY 47 End Get Set(ByVal yValue As Integer) 50 mY = yValue ' no need for validation 51 End Set End Property ' Y ' property Radius 56 Public Property Radius() As Double Get 59 Return mRadius 60 End Get Set(ByVal radiusValue As Double) If radiusValue > 0 65 mRadius = radiusValue 66 End If End Set End Property ' Radius Property of Y Property of Radius Ensures the member mRadius cannot be assigned a negative value

 2002 Prentice Hall. All rights reserved. Outline 11 Circle.vb ' calculate CCircle diameter 73 Public Function Diameter() As Double 74 Return mRadius * 2 75 End Function ' Diameter ' calculate CCircle circumference 78 Public Function Circumference() As Double 79 Return Math.PI * Diameter() 80 End Function ' Circumference ' calculate CCircle area 83 Public Function Area() As Double 84 Return Math.PI * mRadius ^ 2 85 End Function ' Area ' return String representation of CCircle 88 Public Overrides Function ToString() As String 89 Return "Center = " & "[" & mX & ", " & mY & "]" & _ 90 "; Radius = " & mRadius 91 End Function ' ToString End Class ' CCircle Methods to calculate Diameter, Circumference, Area, and ToString representation

 2002 Prentice Hall. All rights reserved. Outline 12 InheritanceTest. vb 1 ' Fig. 9.7: CircleTest.vb 2 ' Testing class CCircle. 3 4 Imports System.Windows.Forms 5 6 Module modCircleTest 7 8 Sub Main() 9 Dim circle As CCircle 10 Dim output As String circle = New CCircle(37, 43, 2.5) ' instantiate CCircle ' get CCircle's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius ' set CCircle's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = ' display CCircle's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf ' display CCircle's diameter 30 output &= "Diameter is " & _ 31 String.Format("{0:F}", circle.Diameter()) & vbCrLf 32 Declares an object of class CCircle Retrieves then concatenate values X, Y and Radius to String output Resets values X, Y and Radius by using its properties Out puts Diameter

 2002 Prentice Hall. All rights reserved. Outline 13 InheritanceTest. vb 33 ' display CCircle's circumference 34 output &= "Circumference is " & _ 35 String.Format("{0:F}", circle.Circumference()) & vbCrLf ' display CCircle's area 38 output &= "Area is " & String.Format("{0:F}", circle.Area()) MessageBox.Show(output, "Demonstrating Class CCircle") 41 End Sub ' Main End Module ' modCircleTest Outputs circumference and area

 2002 Prentice Hall. All rights reserved. Outline 14 Circle2.vb 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() ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New ' constructor 17 Public Sub New(ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double) ' implicit call to CPoint constructor occurs here 21 mX = xValue 22 mY = yValue 23 Radius = radiusValue 24 End Sub ' New ' property Radius 27 Public Property Radius() As Double Get 30 Return mRadius 31 End Get 32 The keyword Inherits indicates inheritance CCircle2 inherits all members of class CPoint except constructors Generates first syntax error

 2002 Prentice Hall. All rights reserved. Outline 15 Circle.vb 33 Set ( ByVal radiusValue As Double ) If radiusValue > 0 36 mRadius = radiusValue 37 End If End Set End Property ' Radius ' calculate CCircle2 diameter 44 Public Function Diameter() As Double 45 Return mRadius * 2 46 End Function ' Diameter ' calculate CCircle2 circumference 49 Public Function Circumference() As Double 50 Return Math. PI * Diameter() 51 End Function ' Circumference ' calculate CCircle2 area 54 Public Function Area() As Double 55 Return Math. PI * mRadius ^ 2 56 End Function ' Area ' return String representation of CCircle2 59 Public Overrides Function ToString() As String 60 Return "Center = " & "[" & mX & ", " & mY & "]" & _ 61 "; Radius = " & mRadius 62 End Function ' ToString End Class ' CCircle2 Generates second syntax error Ensures that variable mRadius cannot be assigned a negative value

 2002 Prentice Hall. All rights reserved. Outline 16 1 ' Fig. 9.9: Point2.vb 2 ' CPoint2 class contains an x-y coordinate pair as Protected data. 3 4 Public Class CPoint2 5 ' implicitly Inherits Object 6 7 ' point coordinate 8 Protected mX, mY As Integer 9 10 ' default constructor 11 Public Sub New() ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 End Sub ' New ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 End Sub ' New ' property X 28 Public Property X() As Integer Get 31 Return mX 32 End Get 33 Class CPoint2 modifies class CPoint to declare variables mX and mY as Protected

 2002 Prentice Hall. All rights reserved. Outline Set(ByVal xValue As Integer) 35 mX = xValue ' no need for validation 36 End Set End Property ' X ' property Y 41 Public Property Y() As Integer Get 44 Return mY 45 End Get Set(ByVal yValue As Integer) 48 mY = yValue ' no need for validation 49 End Set End Property ' Y ' return String representation of CPoint2 54 Public Overrides Function ToString() As String 55 Return "[" & mX & ", " & mY & "]" 56 End Function ' ToString End Class ' CPoint2

 2002 Prentice Hall. All rights reserved. Outline 18 Circle3.vb 1 ' Fig. 9.10: Circle3.vb 2 ' CCircle3 class that inherits from class CPoint Public Class CCircle3 5 Inherits CPoint2 ' CCircle3 Inherits from class CPoint2 6 7 Private m Radius As Double ' CCircle3's radius 8 9 ' default constructor 10 Public Sub New () ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New ' constructor 17 Public Sub New ( ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double ) ' implicit call to CPoint2 constructor occurs here 21 mX = xValue 22 mY = yValue 23 Radius = radiusValue 24 End Sub ' New ' property Radius 27 Public Property Radius() As Double Get 30 Return mRadius 31 End Get Set ( ByVal radiusValue As Double ) 34 Class CCircle3 inherits CPoint2

 2002 Prentice Hall. All rights reserved. Outline 19 CCircle.vb 35 If radiusValue > 0 36 mRadius = radiusValue 37 End If End Set End Property ' Radius ' calculate CCircle3 diameter 44 Public Function Diameter() As Double 45 Return mRadius * 2 46 End Function ' Diameter ' calculate CCircle3 circumference 49 Public Function Circumference() As Double 50 Return Math.PI * Diameter() 51 End Function ' Circumference ' calculate CCircle3 area 54 Public Overridable Function Area() As Double 55 Return Math.PI * mRadius ^ 2 56 End Function ' Area ' return String representation of CCircle3 59 Public Overrides Function ToString() As String 60 Return "Center = " & "[" & mX & ", " & mY & "]" & _ 61 "; Radius = " & mRadius 62 End Function ' ToString End Class ' CCircle3

 2002 Prentice Hall. All rights reserved. Outline 20 CCircle.vb 1 ' Fig. 9.11: CircleTest3.vb 2 ' Testing class CCircle Imports System.Windows.Forms 5 6 Module modCircleTest3 7 8 Sub Main() 9 Dim circle As CCircle3 10 Dim output As String circle = New CCircle3( 37, 43, 2.5 ) ' instantiate CCircle ' get CCircle3's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius ' set CCircle3's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = ' display CCircle3's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf ' display CCircle3's diameter 30 output &= "Diameter is " & _ 31 String.Format( "{0:F}", circle.Diameter()) & vbCrLf 32 Declares an object of class CCircle3 Outputs the X, Y coordinate and the RadiusRedefines X, Y and Radius Calls Diameter function from class CCircle3

 2002 Prentice Hall. All rights reserved. Outline 21 CircleTest.vb 33 ' display CCircle3's circumference 34 output &= "Circumference is " & _ 35 String.Format( "{0:F}", circle.Circumference()) & vbCrLf ' display CCircle3's area 38 output &= "Area is " & String.Format( "{0:F}", circle.Area()) MessageBox.Show(output, "Demonstrating Class CCircle3" ) 41 End Sub ' Main End Module ' modCircleTest3 Calls Circumference and Area function from class CCircle3

 2002 Prentice Hall. All rights reserved. Outline 22 Circle4.vb 1 ' Fig. 9.12: Circle4.vb 2 ' CCircle4 class that inherits from class CPoint. 3 4 Public Class CCircle4 5 Inherits CPoint ' CCircle4 Inherits from class CPoint 6 7 Private mRadius As Double 8 9 ' default constructor 10 Public Sub New() ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New ' constructor 17 Public Sub New(ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double) ' use MyBase reference to CPoint constructor explicitly 21 MyBase.New(xValue, yValue) 22 Radius = radiusValue 23 End Sub ' New ' property Radius 26 Public Property Radius() As Double Get 29 Return mRadius 30 End Get Set(ByVal radiusValue As Double) 33 Class CCircle4 inherits from class CPoint MyBase.New invokes the CPoint constructor explicitly

 2002 Prentice Hall. All rights reserved. Outline 23 Circle4.vb 34 If radiusValue > 0 35 mRadius = radiusValue 36 End If End Set End Property ' Radius ' calculate CCircle diameter 43 Public Function Diameter() As Double 44 Return mRadius * 2 45 End Function ' Diameter ' calculate CCircle4 circumference 48 Public Function Circumference() As Double 49 Return Math.PI * Diameter() 50 End Function ' Circumference ' calculate CCircle4 area 53 Public Overridable Function Area() As Double 54 Return Math.PI * mRadius ^ 2 55 End Function ' Area ' return String representation of CCircle4 58 Public Overrides Function ToString() As String ' use MyBase reference to return CPoint String representation 61 Return "Center= " & MyBase.ToString() & _ 62 "; Radius = " & mRadius 63 End Function ' ToString End Class ' CCircle4 Function ToString overrides function ToString in class CPoint’s

 2002 Prentice Hall. All rights reserved. Outline 24 CircleTest4.vb 1 ' Fig. 9.13: CircleTest4.vb 2 ' Testing class CCircle Imports System.Windows.Forms 5 6 Module modCircleTest4 7 8 Sub Main() 9 Dim circle As CCircle4 10 Dim output As String circle = New CCircle4(37, 43, 2.5) ' instantiate CCircle ' get CCircle4's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius ' set CCircle4's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = ' display CCircle4's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf ' display CCircle4's diameter 30 output &= "Diameter is " & _ 31 String.Format("{0:F}", circle.Diameter()) & vbCrLf 32

 2002 Prentice Hall. All rights reserved. Outline 25 CircleTest4.vb 33 ' display CCircle4's circumference 34 output &= "Circumference is " & _ 35 String.Format("{0:F}", circle.Circumference()) & vbCrLf ' display CCircle4's area 38 output &= "Area is " & String.Format("{0:F}", circle.Area()) MessageBox.Show(output, "Demonstrating Class CCircle4") 41 End Sub ' Main End Module ' modCircleTest4

 2002 Prentice Hall. All rights reserved Case Study: Three-Level Inheritance Hierarchy Inheritance Example: Point-Circle-Cylinder –Point Class CPoint’s Contains instance variables as Private It also contains properties X and Y for accessing mX, mY and method ToString –Circle Class CCircle4 contains the CPoint functionality It also contains property for Radius and methods Diameter, Circumference, Area, and ToString –Cylinder Class CCylinder contains the CCircle functionality It also contains class CPoint indirectly, and it has CCycinlinder constructor method, property Height and method Volume

 2002 Prentice Hall. All rights reserved. Outline 27 Cylinder.vb 1 ' Fig. 9.14: Cylinder.vb 2 ' CCylinder class inherits from class CCircle Public Class CCylinder 5 Inherits CCircle4 6 7 Protected mHeight As Double 8 9 ' default constructor 10 Public Sub New() 11 Height = 0 12 End Sub ' New ' four-argument constructor 15 Public Sub New(ByVal xValue As Integer, _ 16 ByVal yValue As Integer, ByVal radiusValue As Double, _ 17 ByVal heightValue As Double) ' explicit call to CCircle4 constructor 20 MyBase.New(xValue, yValue, radiusValue) 21 Height = heightValue ' set CCylinder height 22 End Sub ' New ' property Height 25 Public Property Height() As Double Get 28 Return mHeight 29 End Get ' set CCylinder height if argument value is positive 32 Set(ByVal heightValue As Double) 33 Class CCylinder inherits CCircle4 MyBase.New invokes the CCircle4 constructor explicitly

 2002 Prentice Hall. All rights reserved. Outline 28 Cylinder.vb 34 If heightValue >= 0 Then 35 mHeight = heightValue 36 End If End Set End Property ' Height ' override method Area to calculate CCylinder area 43 Public Overrides Function Area() As Double 44 Return 2 * MyBase.Area + MyBase.Circumference * mHeight 45 End Function ' Area ' calculate CCylinder volume 48 Public Function Volume() As Double 49 Return MyBase.Area * mHeight 50 End Function ' Volume ' convert CCylinder to String 53 Public Overrides Function ToString() As String 54 Return MyBase.ToString() & "; Height = " & mHeight 55 End Function ' ToString End Class ' CCylinder Method Area overrides method Area in class CCircle4 Method ToString overrides method ToString of class CCircle4

 2002 Prentice Hall. All rights reserved. Outline 29 CylinderTest.vb 1 ' Fig. 9.15: CylinderTest.vb 2 ' Tests class CCylinder. 3 4 Imports System.Windows.Forms 5 6 Module modCylinderTest 7 8 Sub Main() 9 10 ' instantiate object of class CCylinder 11 Dim cylinder As New CCylinder(12, 23, 2.5, 5.7) 12 Dim output As String ' properties get initial x-y coordinate, radius and height 15 output = "X coordinate is " & cylinder.X & vbCrLf & _ 16 "Y coordinate is " & cylinder.Y & vbCrLf & "Radius is " & _ 17 cylinder.Radius & vbCrLf & "Height is " & cylinder.Height ' properties set new x-y coordinate, radius and height 20 cylinder.X = 2 21 cylinder.Y = 2 22 cylinder.Height = cylinder.Radius = ' get new x-y coordinate and radius 26 output &= vbCrLf & vbCrLf & "The new location, radius " & _ 27 "and height of cylinder are" & vbCrLf & "Center = [" & _ 28 cylinder.ToString() & vbCrLf & vbCrLf ' display CCylinder's diameter 31 output &= "Diameter is " & _ 32 String.Format("{0:F}", cylinder.Diameter()) & vbCrLf 33 Declares an object of class CCylinder Outputs values of X, Y, Radius, and Height indirectly Resets the values of the cylinder Invokes method Diameter

 2002 Prentice Hall. All rights reserved. Outline 30 CylinderTest.vb 34 ' display CCylinder's circumference 35 output &= "Circumference is " & _ 36 String.Format("{0:F}", cylinder.Circumference()) & vbCrLf ' display CCylinder's area 39 output &= "Area is " & _ 40 String.Format("{0:F}", cylinder.Area()) & vbCrLf ' display CCylinder's volume 43 output &= "Volume is " & _ 44 String.Format("{0:F}", cylinder.Volume()) MessageBox.Show(output, "Demonstrating Class CCylinder") 47 End Sub ' Main End Module ' modCylinderTest Invokes methods Area, Volume and Circumference

 2002 Prentice Hall. All rights reserved Constructors and Finalizers in Derived Classes Constructors in Derived Classes –Base-class Base-class constructors are not inherited by derived classes Each base-class constructor initializes the base-class instance variables that the derived-class object inherits Finalizers in Derived Classes –Derived Classes Executing the finalizer method should free all resources acquired by the object before the garbage collector reclaims memory for that object Keyword MyBase is used to invoke the finalizer of the base class

 2002 Prentice Hall. All rights reserved. Outline 32 Point3.vb 1 ' Fig. 9.16: Point3.vb 2 ' CPoint3 class represents an x-y coordinate pair. 3 4 Public Class CPoint3 5 6 ' point coordinate 7 Private mX, mY As Integer 8 9 ' default constructor 10 Public Sub New() ' implicit call to Object constructor occurs here 13 X = 0 14 Y = 0 15 Console.Writeline("CPoint3 constructor: {0}", Me) 16 End Sub ' New ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 Console.Writeline("CPoint3 constructor: {0}", Me) 26 End Sub ' New ' finalizer overrides version in class Object 29 Protected Overrides Sub Finalize() 30 Console.Writeline("CPoint3 Finalizer: {0}", Me) 31 MyBase.Finalize() ' call Object finalizer 32 End Sub ' Finalize 33 Me implicitly invokes the class’s ToString method to obtain the String representation of CPoint3’s coordinates Calls method Finalize explicitly

 2002 Prentice Hall. All rights reserved. Outline 33 Point3.vb 34 ' property X 35 Public Property X() As Integer Get 38 Return mX 39 End Get Set(ByVal xValue As Integer) 42 mX = xValue ' no need for validation 43 End Set End Property ' X ' property Y 48 Public Property Y() As Integer Get 51 Return mY 52 End Get Set(ByVal yValue As Integer) 55 mY = yValue ' no need for validation 56 End Set End Property ' Y ' return String representation of CPoint3 61 Public Overrides Function ToString() As String 62 Return "[" & mX & ", " & mY & "]" 63 End Function ' ToString End Class ' CPoint3

 2002 Prentice Hall. All rights reserved. Outline 34 1 ' Fig. 9.17: Circle5.vb 2 ' CCircle5 class that inherits from class CPoint Public Class CCircle5 5 Inherits CPoint3 ' CCircle5 Inherits from class CPoint3 6 7 Private mRadius As Double 8 9 ' default constructor 10 Public Sub New() ' implicit call to CPoint3 constructor occurs here 13 Radius = 0 14 Console.WriteLine("CCircle5 constructor: {0}", Me) 15 End Sub ' New ' constructor 18 Public Sub New(ByVal xValue As Integer, _ 19 ByVal yValue As Integer, ByVal radiusValue As Double) ' use MyBase reference to CPoint3 constructor explicitly 22 MyBase.New(xValue, yValue) 23 Radius = radiusValue 24 Console.WriteLine("CCircle5 constructor: {0}", Me) 25 End Sub ' New ' finalizer overrides version in class CPoint3 28 Protected Overrides Sub Finalize() 29 Console.Writeline("CCircle5 Finalizer: {0}", Me) 30 MyBase.Finalize() ' call CPoint3 finalizer 31 End Sub ' Finalize ' property Radius 34 Public Property Radius() As Double 35 Modified constructor from class CPoint3 that includes Radius Modified constructor from class CPoint3 that includes radiusValue MyBase is keyword that invokes method Finalize

 2002 Prentice Hall. All rights reserved. Outline 35 Circle5.vb 36 Get 37 Return mRadius 38 End Get Set(ByVal radiusValue As Double) If radiusValue > 0 43 mRadius = radiusValue 44 End If End Set End Property ' Radius ' calculate CCircle5 diameter 51 Public Function Diameter() As Double 52 Return mRadius * 2 53 End Function ' Diameter ' calculate CCircle5 circumference 56 Public Function Circumference() As Double 57 Return Math.PI * Diameter() 58 End Function ' Circumference ' calculate CCircle5 area 61 Public Overridable Function Area() As Double 62 Return Math.PI * mRadius ^ 2 63 End Function ' Area 64 Calculates Diameter, Circumference and Area Ensures that variable mRadius cannot be assigned negative value

 2002 Prentice Hall. All rights reserved. Outline 36 Circle5.vb 65 ' return String representation of CCircle5 66 Public Overrides Function ToString() As String ' use MyBase reference to return CPoint3 String 69 Return "Center = " & MyBase.ToString() & _ 70 "; Radius = " & mRadius 71 End Function ' ToString End Class ' CCircle5 Method ToString overrides method ToString of class CPoint3

 2002 Prentice Hall. All rights reserved. Outline 37 1 ' Fig. 9.18: ConstructorAndFinalizer.vb 2 ' Display order in which base-class and derived-class constructors 3 ' and finalizers are called. 4 5 Module modConstructorAndFinalizer 6 7 Sub Main() 8 Dim circle1, circle2 As CCircle circle1 = New CCircle5(72, 29, 4.5) ' instantiate objects 11 circle2 = New CCircle5(5, 5, 10) circle1 = Nothing ' mark objects for garbage collection 14 circle2 = Nothing System.GC.Collect() ' request garbage collector to execute 17 End Sub ' Main End Module ' modConstructorAndFinalizer CPoint3 constructor: Center = [72, 29]; Radius = 0 CCircle5 constructor: Center = [72, 29]; Radius = 4.5 CPoint3 constructor: Center = [5, 5]; Radius = 0 CCircle5 constructor: Center = [5, 5]; Radius = 10 CCircle5 Finalizer: Center = [5, 5]; Radius = 10 CPoint3 Finalizer: Center = [5, 5]; Radius = 10 CCircle5 Finalizer: Center = [72, 29]; Radius = 4.5 CPoint3 Finalizer: Center = [72, 29]; Radius = 4.5 Declares objects of class CCircle5 Assigns it to references circle1 and circle2 Sets circle1 and circle2 to Nothing

 2002 Prentice Hall. All rights reserved Software Engineering with Inheritance Inheritance –Class New classes inherit old classes, hence this method allows programmers to practice software reuse