}

}

Notice that the Age() method increments the age returned by the base class by 1. To use the Director class, create an instance of it and set its date of birth as follows:

Director d = new Director();

d.DateofBirth = new DateTime(1970, 7, 28);

When you print out the age using the Age() method, you get 39 (2008–1970=38; increment it by 1 and the result is 39):

Console.WriteLine(d.Age()); //---39---

This proves that the overriden method in the Age() method is invoked. If you typecast d to the IPerson interface, assign it to an instance of the IPerson interface, and invoke the Age() method, it will still print out 39:

IPerson p = d as IPerson;

Console.WriteLine(p.Age()); //---39---

An interesting thing happens if, instead of overriding the Age() method in the Director class, you create a new Age() class using the new keyword:

public class Director : Employee {

 public new ushort Age() {

  return (ushort)(base.Age() + 1);

 }

}

Create an instance of the Director class and invoke its Age() method; it returns 39, as the following statements show:

Director d = new Director();

d.DateofBirth = new DateTime(1970, 7, 28);

Console.WriteLine(d.Age()); //---39---

However, if you typecast d to an instance of the IPerson interface and then use that interface to invoke the Age() method, you get 38 instead:

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.

Summary

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.

Chapter 6

Inheritance

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.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату