IPerson p = d as IPerson;
Console.WriteLine(p.Age()); //---38---
What's happened is that the instance of the IPerson
interface (p) uses the Age()
method defined in the Employee
class.
An interface defines the contract for a class — the various members that a class must have, the result returned for each method, and so on. However, an interface does not provide the implementation for a class; the actual implementation is left to the implementing classes. This chapter presented different ways in which you can work with interfaces — implementing multiple interfaces, extending interfaces, casting to an interface, and so forth.
Inheritance is one of the fundamental concepts in object-oriented programming. Inheritance facilitates code reuse and allows you to extend the functionality of code that you have already written. This chapter looks at:
□ How inheritance works
□ Implementing inheritance in C#
□ Defining abstract methods and classes
□ Sealing classes and methods
□ Defining overloaded methods
□ The different types of access modifiers you can use in inheritance
□ Using inheritance in interfaces
Understanding Inheritance in C#
The following Employee class contains information about employees in a company:
public class Employee {
public string Name { get; set; }
public DateTime DateofBirth { get; set; }
public ushort Age() {
return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);
}
}
Manager is a class containing information about managers:
public class Manager {
public string Name { get; set; }
public DateTime DateofBirth { get; set; }
public ushort Age() {
return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);
}
public Employee[] subordinates { get; set; }
}
The key difference between the Manager
class and the Employee
class is that Manager
has an additional property, subordinates
, that contains an array of employees under the supervision of a manager. In fact, a manager is actually an employee, except that he has some additional roles. In this example, the Manager
class could inherit from the Employee
class and then add the additional subordinates property that it requires, like this:
public class Manager: Employee {
public Employee[] subordinates { get; set; }
}
By inheriting from the Employee
class, the Manager class has all the members defined in the Employee
class made available to it. The relationships between the Employee
and Manager
classes can be represented using a class diagram as shown in Figure 6-1.
Figure 6-1
Employee
is known as the base class and Manager
is a derived class. In object-oriented programming, inheritance is classified into two types: implementation and interface. This chapter explores both.
Implementation Inheritance
Implementation inheritance is when a class derives from another base class, inheriting all the base class's members. To add new members to a class, you can define another class that derives from the existing base class. Using implementation inheritance, the new derived class inherits all of the implementation provided in the base class.
To understand how inheritance works in C#, define a simple class as follows:
public class Shape {
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}
Here, the Shape
class contains two properties and a single method. By itself, this class does not specify a particular shape, but it does assume that a basic shape contains length and width. It also assumes that the perimeter of a shape is simply double the sum of its length and width.
Using this base class, you can define other shapes such as square, rectangle, and circle. Let's start with the rectangle shape. Using Shape
as the base class, you can define a Rectangle
class (a derived class because it derives from the Shape
class) by inheriting from the Shape
class, like this:
public class Rectangle : Shape {}
In C#, you use the colon ( :
) operator to indicate that a class inherits from another class (known as the base class). This example reads: "The Rectangle
class inherits from the Shape
class." This means that whatever members the Shape
class has are inherited by the Rectangle
class. (In this example, the Rectangle
class has no implementation; that will be added in the next few sections.)
C# supports only single-class inheritance, which means that a class can inherit directly from only one base class. If you do not specify the base class, the C# compiler assumes that it is inheriting from the System.Object
class. Because the Shape
class did not specify who it is inheriting from, it is equivalent to:
public class Shape : Object {
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}
To use the Rectangle
class, you instantiate it as you would other classes:
Rectangle r = new Rectangle();
Because the Rectangle
class inherits all the members of the Shape
class, you can access its members as if they are defined within the Rectangle
class itself:
r.length = 4;
r.width = 5;
Console.WriteLine(r.Perimeter()); //---18---
The Shape
class does not specify a particular shape, and thus it really does not make sense for you to instantiate it directly, like this:
Shape someShape = new Shape();
Instead, all other shapes should inherit from this base class. To ensure that you cannot instantiate the Shape
class directly, you can make it an abstract class by using the abstract
keyword:
public abstract class Shape {
//---properties---
public double length { get; set; }
public double width { get; set; }
//---method---
public double Perimeter() {
return 2 * (this.length + this.width);
}
}
Once a class is defined as abstract, you can no longer instantiate it directly; the following is now not permitted:
//---cannot instantiate directly---
Shape someShape = new Shape();
The abstract
keyword indicates that the class is defined solely for the purpose of inheritance; other classes need to inherit from it in order to have objects of this base type.
Читать дальше