Object-Oriented Programming: Inheritance: Outline
Object-Oriented Programming: Inheritance: Outline
Inheritance
Outline
1.Introduction
1
Introduction to Inheritance
Inheritance allows us to define a class in terms of another
class, which makes it easier to create and maintain an
application. This also provides an opportunity to reuse the
code functionality and speeds up implementation time.
When creating a class, instead of writing completely new
data members and member functions, the programmer can
designate that the new class should inherit the members of
an existing class. This existing class is called
the base class, and the new class is referred to as
the derived 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 access functions
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
3
Base Classes and Derived
Classes
4
Base Classes and Derived
Classes CommunityMemeber
Faculty Staff
Administrator Teacher
TwoDimensionalShape ThreeDimensionalShape
7
Properties and Encapsulation
The meaning of Encapsulation, is to make sure that
"sensitive" data is hidden from users. To achieve this,
you must:
declare fields/variables as private
provide public get and set methods, through properties, to
access and update the value of a private field.
A property is like a combination of a variable and a
method, and it has two methods: a get and a set method.
8
Example Property
class Person static void Main(string[] args)
{ { Person myObj = new Person();
private string name; // field myObj.Name = "Liam";
public string Name // property Console.WriteLine(myObj.Name);
{ get { return name; } // get method }
set { name = value; } // set method
}}
Example explained
•The Name property is associated with the name field. It is a good practice to use
the same name for both the property and the private field, but with an uppercase
first letter.
9
10
1 // Fig. 9.4: Point.cs Outline
2 // Point class represents an x-y coordinate pair.
3
4 using System;
5 Point.cs
6 // Point class definition implicitly inherits from Object
7 public class Point
8 {
X and Y coordinates, declared
9 // point coordinates private so other classes cannot
10 private int x, y; directly access them
11
12 // default (no-argument) constructor
13 public Point()
14 {
15 // implicit call to Object constructor occurs here Default point constructor
16 } with implicit call to
17 Object constructor
18 // constructor
19 public Point( int xValue, int yValue )
20 {
21 // implicit call to Object constructor occurs here Constructor to set
22 X = xValue;
23 Y = yValue; coordinates to parameters,
24 } also has implicit call to
25 Object constructor
26 // property X
27 public int X
28 {
29 get
30 {
31 return x;
32 }
33
2002 Prentice Hall.
All rights reserved.
11
34 set Outline
35 {
36 x = value; // no need for validation
37 }
38 Point.cs
39 } // end property X
40
41 // property Y
42 public int Y
43 {
44 get
45 { Definition of overridden
46 return y;
47 }
method ToString
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
Program Output
Circle2.cs
program output
46