Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 – Object-Oriented Programming: Inheritance

Similar presentations


Presentation on theme: "Chapter 9 – Object-Oriented Programming: Inheritance"— Presentation transcript:

1 Chapter 9 – Object-Oriented Programming: Inheritance
Outline 9.1 Introduction 9.2 Base Classes and Derived Classes 9.3 protected Members 9.4 Creating Base Classes and Derived Classes 9.5 Constructors and Destructors in Derived Classes 9.6 Software Engineering with Inheritance 9.7 Case Study: Point, Circle, Cylinder

2 9.1 Introduction Inheritance:
Classes are created by absorbing the methods and variables of an existing class It then adds its own methods to enhance its capabilities This class is called a derived class because it inherits methods and variables from a base class Objects of derived class are objects of base class, but not vice versa “Is a” relationship: derived class object can be treated as base class object “Has a” relationship: class object has object references as members A derived class can only access non-private base class members unless it inherits accessor funcitons

3 9.2 Base Classes and Derived Classes
An object often is an object of another class Every derived-class is an object of its base class Inheritance forms a tree-like heirarchy To specify class one is derived from class two class one : two Composition: Formed by “has a” relationships Constructors are not inherited

4 9.2 Base Classes and Derived Classes

5 9.2 Base Classes and Derived Classes
CommunityMemeber Employee Student Alumnus Faculty Staff Administrator Teacher Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

6 9.2 Base Classes and Derived Classes
Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Cylinder Fig. 9.3 Portion of a Shape class hierarchy.

7 9.3 protected and internal Members
protected members Can be accessed by base class or any class derived from that base class internal members: Can only be accessed by classed declared in the same assembly Overridden base class members can be accessed: base.member

8 9.4 Relationship between Base Classes and Derived Classes
Use a point-circle hierarchy to represent relationship between base and derived classes The first thing a derived class does is call its base class’ constructor, either explicitly or implicitly override keyword is needed if a derived-class method overrides a base-class method If a base class method is going to be overridden it must be declared virtual

9 Default point constructor with implicit call to Object constructor
1 // Fig. 9.4: Point.cs 2 // Point class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point class definition implicitly inherits from Object 7 public class Point 8 { // point coordinates private int x, y; 11 // default (no-argument) constructor public Point() { // implicit call to Object constructor occurs here } 17 // constructor public Point( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; } 25 // property X public int X { get { return x; } 33 Point.cs X and Y coordinates, declared private so other classes cannot directly access them Default point constructor with implicit call to Object constructor Constructor to set coordinates to parameters, also has implicit call to Object constructor

10 Point.cs Program Output
set { x = value; // no need for validation } 38 } // end property X 40 // property Y public int Y { get { return y; } 48 set { y = value; // no need for validation } 53 } // end property Y 55 // return string representation of Point public override string ToString() { return "[" + x + ", " + y + "]"; } 61 62 } // end class Point Point.cs Program Output Definition of overridden method ToString

11 Calls the ToString method of class Point implicitly PointTest.cs
1 // Fig. 9.5: PointTest.cs 2 // Testing class Point. 3 4 using System; 5 using System.Windows.Forms; 6 7 // PointTest class definition 8 class PointTest 9 { // main entry point for application static void Main( string[] args ) { // instantiate Point object Point point = new Point( 72, 115 ); 15 // display point coordinates via X and Y properties string output = "X coordinate is " + point.X + "\n" + "Y coordinate is " + point.Y; 19 point.X = 10; // set x-coordinate via X property point.Y = 10; // set y-coordinate via Y property 22 // display new point value output += "\n\nThe new location of point is " + point; 25 MessageBox.Show( output, "Demonstrating Class Point" ); 27 } // end method Main 29 30 } // end class PointTest Create a Point object Calls the ToString method of class Point implicitly PointTest.cs Change coordinates of Point object

12 Declare coordinates and radius of circle as private
1 // Fig. 9.6: Circle.cs 2 // Circle class contains x-y coordinate pair and radius. 3 4 using System; 5 6 // Circle class definition implicitly inherits from Object 7 public class Circle 8 { private int x, y; // coordinates of Circle's center private double radius; // Circle's radius 11 // default constructor public Circle() { // implicit call to Object constructor occurs here } 17 // constructor public Circle( int xValue, int yValue, double radiusValue ) { // implicit call to Object constructor occurs here x = xValue; y = yValue; Radius = radiusValue; } 26 // property X public int X { get { return x; } 34 Circle.cs Declare coordinates and radius of circle as private Circle constructors

13 Circle.cs 35 set 36 { 37 x = value; // no need for validation 38 } 39
{ x = value; // no need for validation } 39 } // end property X 41 // property Y public int Y { get { return y; } 49 set { y = value; // no need for validation } 54 } // end property Y 56 // property Radius public double Radius { get { return radius; } 64 set { if ( value >= 0 ) // validation needed radius = value; } Circle.cs

14 Definition of overridden method ToString
70 } // end property Radius 72 // calculate Circle diameter public double Diameter() { return radius * 2; } 78 // calculate Circle circumference public double Circumference() { return Math.PI * Diameter(); } 84 // calculate Circle area public double Area() { return Math.PI * Math.Pow( radius, 2 ); } 90 // return string representation of Circle public override string ToString() { return "Center = [" + x + ", " + y + "]" + "; Radius = " + radius; } 97 98 } // end class Circle Circle.cs Definition of overridden method ToString

15 Change coordinates and radius of Circle object
1 // Fig. 9.7: CircleTest.cs 2 // Testing class Circle. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest class definition 8 class CircleTest 9 { // main entry point for application. static void Main( string[] args ) { // instantiate Circle Circle circle = new Circle( 37, 43, 2.5 ); 15 // get Circle's initial x-y coordinates and radius string output = "X coordinate is " + circle.X + "\nY coordinate is " + circle.Y + "\nRadius is " + circle.Radius; 20 // set Circle's x-y coordinates and radius to new values circle.X = 2; circle.Y = 2; circle.Radius = 4.25; 25 // display Circle's string representation output += "\n\nThe new location and radius of " + "circle are \n" + circle + "\n"; 29 // display Circle's diameter output += "Diameter is " + String.Format( "{0:F}", circle.Diameter() ) + "\n"; 33 Create a Circle object CircleTest.cs Change coordinates and radius of Circle object Implicit call to circle’s ToString method

16 Call Circle’s Circumference and Area methods for output CircleTest.cs
// display Circle's circumference output += "Circumference is " + String.Format( "{0:F}", circle.Circumference() ) + "\n"; 37 // display Circle's area output += "Area is " + String.Format( "{0:F}", circle.Area() ); 41 MessageBox.Show( output, "Demonstrating Class Circle" ); 43 } // end method Main 45 46 } // end class CircleTest Call Circle’s Circumference and Area methods for output CircleTest.cs

17 Declare class Circle to derive from class Point
1 // Fig. 9.8: Circle2.cs 2 // Circle2 class that inherits from class Point. 3 4 using System; 5 6 // Circle2 class definition inherits from Point 7 class Circle2 : Point 8 { private double radius; // Circle2's radius 10 // default constructor public Circle2() { // implicit call to Point constructor occurs here } 16 // constructor public Circle2( int xValue, int yValue, double radiusValue ) { // implicit call to Point constructor occurs here x = xValue; y = yValue; Radius = radiusValue; } 25 // property Radius public double Radius { get { return radius; } 33 Declare class Circle to derive from class Point Circle2.cs Declare radius as private Implicit calls to base class constructor Attempt to directly change private base class methods results in an error

18 set { if ( value >= 0 ) radius = value; } 39 } // end property Radius 41 // calculate Circle diameter public double Diameter() { return radius * 2; } 47 // calculate Circle circumference public double Circumference() { return Math.PI * Diameter(); } 53 // calculate Circle area public virtual double area() { return Math.PI * Math.Pow( radius, 2 ); } 59 // return string representation Circle public override string ToString() { return "Center = [" + x + ", " + y + "]" + "; Radius = " + radius; } 66 67 } // end class Circle2 Circle2.cs Attempt to directly access private base class members results in an error

19 Circle2.cs program output

20 1 // Fig. 9.9: Point2.cs 2 // Point2 class contains an x-y coordinate pair as protected data. 3 4 using System; 5 6 // Point2 class definition implicitly inherits from Object 7 public class Point2 8 { // point coordinate protected int x, y; 11 // default constructor public Point2() { // implicit call to Object constructor occurs here } 17 // constructor public Point2( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; } 25 // property X public int X { get { return x; } 33 Point2.cs Declare coordinates as protected so derived classes can directly access them

21 Point2.cs 34 set 35 { 36 x = value; // no need for validation 37 } 38
{ x = value; // no need for validation } 38 } // end property X 40 // property Y public int Y { get { return y; } 48 set { y = value; // no need for validation } 53 } // end property Y 55 // return string representation of Point2 public override string ToString() { return "[" + x + ", " + y + "]"; } 61 62 } // end class Point2 Point2.cs

22 Class Circle3 inherits from Point2
1 // Fig. 9.10: Circle3.cs 2 // Circle2 class that inherits from class Point2. 3 4 using System; 5 6 // Circle3 class definition inherits from Point2 7 public class Circle3 : Point2 8 { private double radius; // Circle's radius 10 // default constructor public Circle3() { // implicit call to Point constructor occurs here } 16 // constructor public Circle3( int xValue, int yValue, double radiusValue ) { // implicit call to Point constructor occurs here x = xValue; y = yValue; Radius = radiusValue; } 26 // property Radius public double Radius { get { return radius; } 34 Circle3.cs Class Circle3 inherits from Point2 Directly changing protected base class members does not result in error

23 Directly accessing protected members does not result in error
set { if ( value >= 0 ) radius = value; } 40 } // end property Radius 42 // calculate Circle diameter public double Diameter() { return radius * 2; } 48 // calculate circumference public double Circumference() { return Math.PI * Diameter(); } 54 // calculate Circle area public virtual double Area() { return Math.PI * Math.Pow( radius, 2 ); } 60 // return string representation of Circle3 public override string ToString() { return "Center = [" + x + ", " + y + "]" + "; Radius = " + radius; } 67 68 } // end class Circle3 Circle3.cs Directly accessing protected members does not result in error

24 Change coordinates and radius of Circle3 object
1 / Fig. 9.11: CircleTest3.cs 2 // Testing class Circle3. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest3 class definition 8 class CircleTest3 9 { // main entry point for application static void Main( string[] args ) { // instantiate Circle3 Circle3 circle = new Circle3( 37, 43, 2.5 ); 15 // get Circle3's initial x-y coordinates and radius string output = "X coordinate is " + circle.X + "\n" + "Y coordinate is " + circle.Y + "\nRadius is " + circle.Radius; 20 // set Circle3's x-y coordinates and radius to new values circle.X = 2; circle.Y = 2; circle.Radius = 4.25; 25 // display Circle3's string representation output += "\n\n" + "The new location and radius of circle are " + "\n" + circle + "\n"; 30 // display Circle3's Diameter output += "Diameter is " + String.Format( "{0:F}", circle.Diameter() ) + "\n"; 34 CircleTest3.cs Change coordinates and radius of Circle3 object Create new Circle3 object Implicit call to Circle3’s ToString method

25 Call Circle’s Circumference and Area methods for output CircleTest3.cs
// display Circle3's Circumference output += "Circumference is " + String.Format( "{0:F}", circle.Circumference() ) + "\n"; 38 // display Circle3's Area output += "Area is " + String.Format( "{0:F}", circle.Area() ); 42 MessageBox.Show( output, "Demonstrating Class Circle3" ); 44 } // end method Main 46 47 } // end class CircleTest3 Call Circle’s Circumference and Area methods for output CircleTest3.cs

26 Declare coordinates as private
1 // Fig. 9.12: Point3.cs 2 // Point3 class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point3 class definition implicitly inherits from Object 7 public class Point3 8 { // point coordinate private int x, y; 11 // default constructor public Point3() { // implicit call to Object constructor occurs here } 17 // constructor public Point3( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; // use property X Y = yValue; // use property Y } 25 // property X public int X { get { return x; } 33 Point3.cs Declare coordinates as private

27 Methods to set x and y coordinates
{ x = value; // no need for validation } 38 } // end property X 40 // property Y public int Y { get { return y; } 48 set { y = value; // no need for validation } 53 } // end property Y 55 // return string representation of Point3 public override string ToString() { return "[" + X + ", " + Y + "]"; } 61 62 } // end class Point3 Point3.cs Methods to set x and y coordinates Overridden ToString method

28 Constructor with implicit call to base class constructor
1 // Fig. 9.13: Circle4.cs 2 // Circle4 class that inherits from class Point3. 3 4 using System; 5 6 // Circle4 class definition inherits from Point3 7 public class Circle4 : Point3 8 { private double radius; 10 // default constructor public Circle4() { // implicit call to Point constructor occurs here } 16 // constructor public Circle4( int xValue, int yValue, double radiusValue ) : base( xValue, yValue ) { Radius = radiusValue; } 23 // property Radius public double Radius { get { return radius; } 31 set { if ( value >= 0 ) // validation needed radius = value; Circle4.cs Constructor with implicit call to base class constructor Constructor with explicit call to base class constructor Explicit call to base class constructor

29 Method area declared virtual so it can be overridden
} 37 } // end property Radius 39 // calculate Circle diameter public double Diameter() { return Radius * 2; // use property Radius } 45 // calculate Circle circumference public double Circumference() { return Math.PI * Diameter(); } 51 // calculate Circle area public virtual double Area() { return Math.PI * Math.Pow( Radius, 2 ); // use property } 57 // return string representation of Circle4 public override string ToString() { // use base reference to return Point string representation return "Center= " + base.ToString() + "; Radius = " + Radius; // use property Radius } 65 66 } // end class Circle4 Circle4.cs Method area declared virtual so it can be overridden Circle4’s ToString method overrides Point3’s ToString method Call Point3’s ToString method to display coordinates

30 Change coordinates and radius of Circle4 object
1 // Fig. 9.14: CircleTest4.cs 2 // Testing class Circle4. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest4 class definition 8 class CircleTest4 9 { // main entry point for application static void Main( string[] args ) { // instantiate Circle4 Circle4 circle = new Circle4( 37, 43, 2.5 ); 15 // get Circle4's initial x-y coordinates and radius string output = "X coordinate is " + circle.X + "\n" + "Y coordinate is " + circle.Y + "\n" + "Radius is " + circle.Radius; 20 // set Circle4's x-y coordinates and radius to new values circle.X = 2; circle.Y = 2; circle.Radius = 4.25; 25 // display Circle4's string representation output += "\n\n" + "The new location and radius of circle are " + "\n" + circle + "\n"; 30 // display Circle4's Diameter output += "Diameter is " + String.Format( "{0:F}", circle.Diameter() ) + "\n"; 34 CircleTest4.cs Change coordinates and radius of Circle4 object Create new Circle4 object Implicit call to Circle4’s ToString method

31 Call Circle’s Circumference and Area methods for output CircleTest4.cs
// display Circle4's Circumference output += "Circumference is " + String.Format( "{0:F}", circle.Circumference() ) + "\n"; 38 // display Circle4's Area output += "Area is " + String.Format( "{0:F}", circle.Area() ); 42 MessageBox.Show( output, "Demonstrating Class Circle4" ); 44 } // end method Main 46 47 } // end class CircleTest4 Call Circle’s Circumference and Area methods for output CircleTest4.cs

32 9.5 Case Study: Three-Level Inheritance Hierarchy
Three-level inheritance example: Class Cylinder inherits from class Circle4 Class Circle4 inherits from class Point3

33 Class Cylinder inherits from class Circle4
1 // Fig. 9.15: Cylinder.cs 2 // Cylinder class inherits from class Circle4. 3 4 using System; 5 6 // Cylinder class definition inherits from Circle4 7 public class Cylinder : Circle4 8 { private double height; 10 // default constructor public Cylinder() { // implicit call to Circle4 constructor occurs here } 16 // four-argument constructor public Cylinder( int xValue, int yValue, double radiusValue, double heightValue ) : base( xValue, yValue, radiusValue ) { Height = heightValue; // set Cylinder height } 23 // property Height public double Height { get { return height; } 31 set { if ( value >= 0 ) // validate height height = value; Cylinder.cs Class Cylinder inherits from class Circle4 Declare variable height as private Constructor that implicitly calls base class constructor Constructor that explicitly calls base class constructor

34 Method Area overrides Circle4’s Area method
} 37 } // end property Height 39 // override Circle4 method Area to calculate Cylinder area public override double Area() { return 2 * base.Area() + base.Circumference() * Height; } 45 // calculate Cylinder volume public double Volume() { return base.Area() * Height; } 51 // convert Cylinder to string public override string ToString() { return base.ToString() + "; Height = " + Height; } 57 58 } // end class Cylinder Cylinder.cs Method Area overrides Circle4’s Area method Calculate volume of cylinder Overridden ToString method Call Circle4’s ToString method to get its output

35 Change coordinates, radius and height
1 // Fig. 9.16: CylinderTest.cs 2 // Tests class Cylinder. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CylinderTest class definition 8 class CylinderTest 9 { // main entry point for application static void Main( string[] args ) { // instantiate object of class Cylinder Cylinder cylinder = new Cylinder(12, 23, 2.5, 5.7); 15 // properties get initial x-y coordinate, radius and height string output = "X coordinate is " + cylinder.X + "\n" + "Y coordinate is " + cylinder.Y + "\nRadius is " + cylinder.Radius + "\n" + "Height is " + cylinder.Height; 20 // properties set new x-y coordinate, radius and height cylinder.X = 2; cylinder.Y = 2; cylinder.Radius = 4.25; cylinder.Height = 10; 26 // get new x-y coordinate and radius output += "\n\nThe new location, radius and height of " + "cylinder are\n" + cylinder + "\n\n"; 30 // display Cylinder's Diameter output += "Diameter is " + String.Format( "{0:F}", cylinder.Diameter() ) + "\n"; 34 CylinderTest.cs Create new cylinder Change coordinates, radius and height Implicit call to ToString

36 Call methods Circumference, Area and Volume CylinderTest.cs
// display Cylinder's Circumference output += "Circumference is " + String.Format( "{0:F}", cylinder.Circumference() ) + "\n"; 38 // display Cylinder's Area output += "Area is " + String.Format( "{0:F}", cylinder.Area() ) + "\n"; 42 // display Cylinder's Volume output += "Volume is " + String.Format( "{0:F}", cylinder.Volume() ); 46 MessageBox.Show( output, "Demonstrating Class Cylinder" ); 48 } // end method Main 50 51 } // end class CylinderTest Call methods Circumference, Area and Volume CylinderTest.cs

37 9.6 Constructors and Destructors in Derived Classes
Instantiating a derived class, causes base class constructor to be called, implicitly or explicitly Can cause chain reaction when a base class is also a derived class When a destructor is called, it performs its task and then invokes the derived class’ base class constructor

38 Destructor with output message
1 // Fig. 9.17: Point4.cs 2 // Point4 class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point4 class definition 7 public class Point4 8 { // point coordinate private int x, y; 11 // default constructor public Point4() { // implicit call to Object constructor occurs here Console.WriteLine( "Point4 constructor: {0}", this ); } 18 // constructor public Point4( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; Console.WriteLine( "Point4 constructor: {0}", this ); } 27 // destructor ~Point4() { Console.WriteLine( "Point4 destructor: {0}", this ); } 33 // property X public int X Point4.cs Constructors with output messages and implicit calls to base class constructor Output statements use reference this to implicitly call ToString method Destructor with output message

39 Point4.cs 36 { 37 get 38 { 39 return x; 40 } 41 42 set 43 {
{ get { return x; } 41 set { x = value; // no need for validation } 46 } // end property X 48 // property Y public int Y { get { return y; } 56 set { y = value; // no need for validation } 61 } // end property Y 63 // return string representation of Point4 public override string ToString() { return "[" + x + ", " + y + "]"; } 69 70 } // end class Point4 Point4.cs

40 Constructors with calls to base class and output statements
1 // Fig. 9.18: Circle5.cs 2 // Circle5 class that inherits from class Point4. 3 4 using System; 5 6 // Circle5 class definition inherits from Point4 7 public class Circle5 : Point4 8 { private double radius; 10 // default constructor public Circle5() { // implicit call to Point3 constructor occurs here Console.WriteLine( "Circle5 constructor: {0}", this ); } 17 // constructor public Circle5( int xValue, int yValue, double radiusValue ) : base( xValue, yValue ) { Radius = radiusValue; Console.WriteLine( "Circle5 constructor: {0}", this ); } 25 // destructor overrides version in class Point4 ~Circle5() { Console.WriteLine( "Circle5 destructor: {0}", this ); } 31 // property Radius public double Radius { Circle5.cs Constructors with calls to base class and output statements Output statements use reference this to implicitly call ToString method Destructor with output message

41 Circle5.cs 35 get 36 { 37 return radius; 38 } 39 40 set 41 {
{ return radius; } 39 set { if ( value >= 0 ) radius = value; } 45 } // end property Radius 47 // calculate Circle5 diameter public double Diameter() { return Radius * 2; } 53 // calculate Circle5 circumference public double Circumference() { return Math.PI * Diameter(); } 59 // calculate Circle5 area public virtual double Area() { return Math.PI * Math.Pow( Radius, 2 ); } 65 // return string representation of Circle5 public override string ToString() { Circle5.cs

42 Circle5.cs 69 // use base reference to return Point3 string
return "Center = " + base.ToString() + "; Radius = " + Radius; } 73 74 } // end class Circle5 Circle5.cs

43 Create two objects of type Circle5
1 // Fig. 9.19: ConstructorAndDestructor.cs 2 // Display order in which base-class and derived-class constructors 3 // and destructors are called. 4 5 using System; 6 7 // ConstructorAndFinalizer class definition 8 class ConstructorAndFinalizer 9 { // main entry point for application. static void Main( string[] args ) { Circle5 circle1, circle2; 14 // instantiate objects circle1 = new Circle5( 72, 29, 4.5 ); circle2 = new Circle5( 5, 5, 10 ); 18 Console.WriteLine(); 20 // mark objects for garbage collection circle1 = null; circle2 = null; 24 // inform garbage collector to execute System.GC.Collect(); 27 } // end method Main 29 } // end class ConstructorAndDestructor ConstructorAndDestructor.cs Create two objects of type Circle5 Remove references to Circle5 objects Run the garbage collector

44 ConstructorAndDestructor.cs program output
Point4 constructor: Center = [72, 29]; Radius = 0 Circle5 constructor: Center = [72, 29]; Radius = 4.5 Point4 constructor: Center = [5, 5]; Radius = 0 Circle5 constructor: Center = [5, 5]; Radius = 10 Circle5 destructor: Center = [5, 5]; Radius = 10 Point4 destructor: Center = [5, 5]; Radius = 10 Circle5 destructor: Center = [72, 29]; Radius = 4.5 Point4 destructor: Center = [72, 29]; Radius = 4.5 ConstructorAndDestructor.cs program output

45 9.7 Software Engineering with Inheritance
Can customize derived classes to meet needs by: Creating new member variables Creating new methods Override base-class members .NET Framework Class Library(FCL) allows full reuse of software through inheritance


Download ppt "Chapter 9 – Object-Oriented Programming: Inheritance"

Similar presentations


Ads by Google