Extending Interfaces
You can extend interfaces if you need to add new members to an existing interface. For example, you might want to define another interface named IManager
to store information about managers. Basically, a manager uses the same members defined in the IPerson
interface, with perhaps just one more additional property — Dept
. In this case, you can define the IManager
interface by extending the IPerson
interface, like this:
interface IPerson {
string Name { get; set; }
DateTime DateofBirth { get; set; }
ushort Age();
}
interface IManager : IPerson {
string Dept { get; set; }
}
To use the IManager
interface, you define a Manager
class that implements the IManager
interface, like this:
public class Manager : IManager {
//---IPerson---
public string Name { get; set; }
public DateTime DateofBirth { get; set; }
public ushort Age() {
return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);
}
//---IManager---
public string Dept { get; set; }
}
The Manager
class now implements all the members defined in the IPerson
interface, as well as the additional member defined in the IManager
interface. You can use the Manager
class like this:
Manager m1 = new Manager() {
Name = 'John',
DateofBirth = new DateTime(1970, 7, 28),
Dept = 'IT'
};
Console.WriteLine(m1.Age());
You can also extend multiple interfaces at the same time. The following example shows the IManager
interface extending both the IPerson
and the IAddress
interfaces:
string Dept { get; set; }
}
The Manager
class now needs to implement the additional members defined in the IAddress
interface:
public class Manager : IManager {
//---IPerson---
public string Name { get; set; }
public DateTime DateofBirth { get; set; }
public ushort Age() {
return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);
}
//---IManager---
public string Dept { get; set; }
}
You can now access the Manager
class like this:
Manager m1 = new Manager() {
Name = 'John',
DateofBirth = new DateTime(1970, 7, 28),
Dept = 'IT',
Street = 'Kingston Street',
Zip = 12345
};
Console.WriteLine(m1.Age());
Console.WriteLine(m1.State());
Interface Casting
In the preceding example, the IManager interface extends both the IPerson
and IAddress
interfaces. So an instance of the Manager
class (which implements the IManager
interface) will contain members defined in both the IPerson
and IAddress
interfaces:
Manager m1 = new Manager() {
Name = 'John', //---from IPerson---
DateofBirth = new DateTime(l970, 7, 28), //---from IPerson---
Dept = 'IT', //---from IManager---
Street = 'Kingston Street', //---from IAddress---
Zip = 12345 //---from IAddress---
};
Console.WriteLine(m1.Age()); //---from IPerson---
Console.WriteLine(m1.State()); //---from IAddress---
In addition to accessing the members of the Manager
class through its instance (in this case m1
), you can access the members through the interface that it implements. For example, since m1
is a Manager
object that implements both the IPerson
and IAddress
interfaces, you can cast m1
to the IPerson
interface and then assign it to a variable of type IPerson
, like this:
//---cast to IPerson---
IPerson p = (IPerson) m1;
This is known as interface casting. Interface casting allows you to cast an object to one of its implemented interfaces and then access its members through that interface.