Chapter 4. Understanding
Object-Oriented
Programming: Inheritance
Hoang Anh Viet
Hanoi University of Technology
1
MicrosoftMicrosoft
Objectives
“This chapter introduced inheritancethe ability to create classes by
absorbing an existing class's members and enhancing them with new
capabilities. You learned the notions of base classes and derived
classes and created a derived class that inherits members from a base
class. The chapter introduced access modifier protected; derived class
methods can access protected base class members. You learned how to
access base class members with base. You also saw how constructors are
used in inheritance hierarchies. Finally, you learned about Software
Engineering with Inheritance.”
Introduction
Base Classes and Derived Classes
protected and internal Members
Relationship between Base Classes and Derived Classes
Constructors and Destructors in Derived Classes
Extension methods and Inheritance
Software Engineering with Inheritance
2
MicrosoftMicrosoft
Roadmap
4.1 Introduction
4.2 Base Classes and Derived Classes
4.3 protected and internal Members
4.4 Relationship between Base Classes and Derived Classes
4.5 Constructors and Destructors in Derived Classes
4.6 Extension methods and Inheritance
4.7 Software Engineering with Inheritance
3
MicrosoftMicrosoft
4.1 Introduction
Defining the Pillars of OOP:
Encapsulation: How does this language hide an object’s
internal implementation details and preserve data integrity?
Inheritance: How does this language promote code reuse?
Polymorphism: How does this language let you treat related
objects in a similar way?
The Role of Inheritance:
In essence, inheritance allows you to extend the behavior of a
base (or parent) class by inheriting core functionality into the
derived subclass (also called a child class)
4
MicrosoftMicrosoft
4.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 (inheritance)
“Has a” relationship: class object has object references as
members (composition)
A derived class can only access non-private base class
members unless it inherits accessor funcitons
5
MicrosoftMicrosoft
4.1 Introduction
Types Of Inheritance: what C# does and does not support ?
Implementation vs. Interface Inheritance
Implementation inheritance means that a type
derives from a base type, taking all the basetype's
member fields and functions.
Interface inheritance means that a type inherits only
the signatures of the functions, but does not inherit
any implementations.
Multiple Inheritance: C# does not support multiple implementation
inheritance. It does, however, allow types to derive from multiple
interfaces.
Structs and Classes: structs do not support inheritance.
6
MicrosoftMicrosoft
Roadmap
4.1 Introduction
4.2 Base Classes and Derived Classes
4.3 protected and internal Members
4.4 Relationship between Base Classes and Derived Classes
4.5 Constructors and Destructors in Derived Classes
4.6 Extension methods and Inheritance
4.7 Software Engineering with Inheritance
7
MicrosoftMicrosoft
4.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
8
MicrosoftMicrosoft
4.2 Base Classes and Derived Classes
9
MicrosoftMicrosoft
4.2 Base Classes and Derived Classes
Figure 4.2. UML class diagram showing an inheritance hierarchy
for university CommunityMembers
10
MicrosoftMicrosoft
4.2 Base Classes and Derived Classes
Figure 4.3. UML class diagram showing an inheritance hierarchy for
Shapes
11
MicrosoftMicrosoft
12
4.2 Base Classes and Derived Classes
12
public inheritance
Specify with:
Class TwoDimensionalShape : public Shape
Class TwoDimensionalShape inherits from class
Shape
Base class private members
Not accessible directly
Still inherited
Manipulate through inherited member functions
Base class public and protected members
Inherited with original member access
friend functions
Not inherited
MicrosoftMicrosoft
Roadmap
4.1 Introduction
4.2 Base Classes and Derived Classes
4.3 protected and internal Members
4.4 Relationship between Base Classes and Derived Classes
4.5 Constructors and Destructors in Derived Classes
4.6 Extension methods and Inheritance
4.7 Software Engineering with Inheritance
13
MicrosoftMicrosoft
14
4.3 Protected and internal Members
14
We know public and private members of a base class
public member: accessible anywhere that the program has a
reference to an object of that base class or one of its derived
classes
private member: accessible only within the body of that base
class
MicrosoftMicrosoft
15
4.3 Protected and internal Members
15
Now two intermediate (between public and private) levels of protection for
members of a base class :
protected member: accessible by base class or any class derived
from that base class
internal members: accessible in any part of the assembly in
which the member is declared
Recall: The assembly = a package containing the MS
Intermediate Language (MISL) code that a project has been
compiled into, plus any other info that is needed for its classes
Overridden base class members can be accessed using:
base.member (e.g., base.ToString) - ‘base’ is the keyword
MicrosoftMicrosoft
16
4.3 Protected and internal Members
16
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
Calls either explicitly or implicitly
override keyword is needed if a derived-class method
overrides a base-class method
E.g, public override double Area()
If a base class method is going to be overridden it must be
declared virtual
E.g., public virtual double area()
MicrosoftMicrosoft
1717
1 // Point.cs
2 // Point class represents an x-y coordinate pair.
3
4 using System;
5
6 // Point class definition implicitly inherits from Object (System.Object)
7 public class Point
8 {
9 // point coordinates
10 private int x, y;
11
12 // default (no-argument) constructor
13 public Point()
14 {
15 // implicit call to Object constructor occurs here
16 }
17
18 // constructor
19 public Point( int xValue, int yValue )
20 {
21 // implicit call to Object constructor occurs here
22 X = xValue;
23 Y = yValue;
24 }
25
26 // property X
27 public int X
28 {
29 get
30 {
31 return x;
32 }
33
X and Y coordinates, declared
private so other classes cannot
directly access them
Default point constructor
with implicit call to
System’s Object
constructor
Constructor to set
coordinates to parameters,
also has implicit call to
System’s Object
constructor
X and Y coordinates,
declared private so other
classes cannot directly
access them
Default point
constructor with
implicit call to System’s
Object constructor
4.3 Protected and internal Members
MicrosoftMicrosoft
1818
34 set
35 {
36 x = value; // no need for validation
37 }
38
39 } // end property X
40
41 // property Y
42 public int Y
43 {
44 get
45 {
46 return y;
47 }
48
49 set
50 {
51 y = value; // no need for validation
52 }
53
54 } // end property Y
55
56 // return string representation of Point
57 public override string ToString()
58 {
59 return "[" + x + ", " + y + "]";
60 }
61
62 } // end class Point
Definition of overridden
method ToString (overrides
System’s Object.ToString)
4.3 Protected and internal Members
MicrosoftMicrosoft
19
4.3 Protected and internal Members
19
Object
(namespace: System)
Point –
p.348-ed.1
(private x, y)
implicit inheritance
MicrosoftMicrosoft
20
4.3 Protected and internal Members
20
1 // PointTest.cs
2 // Testing class Point.
3
4 using System;
5 using System.Windows.Forms;
6
7 // PointTest class definition
8 class PointTest
9 {
10 // main entry point for application
11 static void Main( string[] args )
12 {
13 // instantiate Point object
14 Point point = new Point( 72, 115 );
15
16 // display point coordinates via X and Y properties
17 string output = "X coordinate is " + point.X +
18 "\n" + "Y coordinate is " + point.Y;
19
20 point.X = 10; // set x-coordinate via X property
21 point.Y = 10; // set y-coordinate via Y property
22
23 // display new point value
24 output += "\n\nThe new location of point is " + point;
25
26 MessageBox.Show( output, "Demonstrating Class Point" );
27
28 } // end method Main
29
30 } // end class PointTest
Calls the ToString method
of class Point implicitly
(converts ‘point’ to string,
bec. output is a string)
Create a Point
object
Change coordinates of Point object
MicrosoftMicrosoft
2121
4.3 Protected and internal Members
1 // 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 {
9 private int x, y; // coordinates of Circle's center
10 private double radius; // Circle's radius
11
12 // default constructor
13 public Circle()
14 {
15 // implicit call to Object constructor occurs here
16 }
17
18 // constructor
19 public Circle( int xValue, int yValue, double radiusValue )
20 {
21 // implicit call to Object constructor occurs here
22 x = xValue;
23 y = yValue;
24 Radius = radiusValue;
25 }
26
27 // property X
28 public int X
29 {
30 get
31 {
32 return x;
33 }
34
Declare coordinates and
radius of circle as
private
Circle
constructors
MicrosoftMicrosoft
2222
4.3 Protected and internal Members
35 set
36 {
37 x = value; // no need for validation
38 }
39
40 } // end property X
41
42 // property Y
43 public int Y
44 {
45 get
46 {
47 return y;
48 }
49
50 set
51 {
52 y = value; // no need for validation
53 }
54
55 } // end property Y
56
57 // property Radius
58 public double Radius
59 {
60 get
61 {
62 return radius;
63 }
64
65 set
66 {
67 if ( value >= 0 ) // validation needed
68 radius = value;
69 }
MicrosoftMicrosoft
2424
4.3 Protected and internal Members
Object
(namespace: System)
Point
– p.348-ed.1
(private x, y)
Circle
- p.351-ed.1
implicit inheritance
MicrosoftMicrosoft
2525
1 // CircleTest.cs
2 // Testing class Circle.
3
4 using System;
5 using System.Windows.Forms;
6
7 // CircleTest class definition
8 class CircleTest
9 {
10 // main entry point for application.
11 static void Main( string[] args )
12 {
13 // instantiate Circle
14 Circle circle = new Circle( 37, 43, 2.5 );
15
16 // get Circle's initial x-y coordinates and radius
17 string output = "X coordinate is " + circle.X +
18 "\nY coordinate is " + circle.Y + "\nRadius is " +
19 circle.Radius;
20
21 // set Circle's x-y coordinates and radius to new values
22 circle.X = 2;
23 circle.Y = 2;
24 circle.Radius = 4.25;
25
26 // display Circle's string representation
27 output += "\n\nThe new location and radius of " +
28 "circle are \n" + circle + "\n";
29
30 // display Circle's diameter
31 output += "Diameter is " +
32 String.Format( "{0:F}", circle.Diameter() ) + "\n";
33
Change coordinates and
radius of Circle object
Implicit call to
circle’s ToString
method (converts
circle to string
bec. output is a
string)
Create a Circle object
4.3 Protected and internal Members
MicrosoftMicrosoft
31
1 // 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 {
9 // point coordinate
10 protected int x, y;
11
12 // default constructor
13 public Point2()
14 {
15 // implicit call to Object constructor occurs here
16 }
17
18 // constructor
19 public Point2( int xValue, int yValue )
20 {
21 // implicit call to Object constructor occurs here
22 X = xValue;
23 Y = yValue;
24 }
25
26 // property X
27 public int X
28 {
29 get
30 {
31 return x;
32 }
33
Declare coordinates as
protected so derived
classes can directly access
them (they were private
in Point.cs )
4.3 Protected and internal Members