Implementing Multiple Interfaces
Some important things to implement multiple interfaces:
Modified code will display following output:
- A class or struct can implement any number of interfaces.
- All the interfaces implemented must be listed in the base class list and separated by commas (following the base class name, if there is one).
public interface IPrintIdentity { string getName(); int getAge(); void printIdentity(); } public interface IBankAccount //new interface declaration { void printAccountBalance(); } public class date { public int year, month, day; public date(int Year,int Month,int Day) { year = Year; month = Month; day = Day; } } public class person:IPrintIdentity,IBankAccount { public string firstName; public string lastName; public date bornDate; public int accountBalance; public string getName() { return firstName + " " + lastName; } public int getAge() { return DateTime.Now.Year - bornDate.year; } public void printIdentity() { Console.WriteLine("Person's Name:" + getName()); Console.WriteLine("Age:" + getAge()); } public void printAccountBalance() { Console.WriteLine("Account Balance" + accountBalance + " USD"); } } public class company : IPrintIdentity,IBankAccount { public string name; public date builtDate; public int accountBalance; public string getName() { return name; } public int getAge() { return DateTime.Now.Year - builtDate.year; } public void printIdentity() { Console.WriteLine("Company's Name:" + getName()); Console.WriteLine("Age:" + getAge()); } public void printAccountBalance() { Console.WriteLine("Account Balance" + accountBalance + " USD"); } } class Program { static void Main(string[] args) { person me = new person { firstName = "Yang", lastName = "Sopiana", bornDate = new date(1986, 11, 2),accountBalance=200 }; company myCompany = new company { name = "Samsung Electronics Indonesia", builtDate = new date(1990, 11, 20) ,accountBalance=1900}; me.printIdentity(); me.printAccountBalance(); myCompany.printIdentity(); myCompany.printAccountBalance(); Console.Read(); } }
Modified code will display following output:
Person's Name:Yang Sopiana
Age:29
Account Balance200 USD
Company's Name:Samsung Electronics Indonesia
Age:25
Account Balance1900 USD
Age:29
Account Balance200 USD
Company's Name:Samsung Electronics Indonesia
Age:25
Account Balance1900 USD
Implementing Interfaces with Duplicate Members
Since a class can implement any number of interfaces, it’s possible that two or more of the interface members might have the same signature and return type.
So, how does the compiler handle that situation?
For example, how if we add one new function in
The answer is that if a class implements multiple interfaces, where several of the interfaces have members with the same signature and return type, the class can implement a single member that satisfies all the interfaces containing that duplicated member.
For example, how if we add one new function in
IBankAccount
as follow:
public interface IPrintIdentity { string getName(); int getAge(); void printIdentity(); //same method's name } public interface IBankAccount { void printIdentity(); //same method's name void printAccountBalance(); }
The answer is that if a class implements multiple interfaces, where several of the interfaces have members with the same signature and return type, the class can implement a single member that satisfies all the interfaces containing that duplicated member.
public class person:IPrintIdentity,IBankAccount { .... public void printIdentity() { Console.WriteLine("Person's Name:" + getName()); Console.WriteLine("Age:" + getAge()); } .... } public class company : IPrintIdentity,IBankAccount { .... public void printIdentity() // Single implementation for both IPrintIdentity and IBankAccount { Console.WriteLine("Company's Name:" + getName()); Console.WriteLine("Age:" + getAge()); } .... }
Explicit Interface Member Implementations
A class can implements duplicate interface members with a single implementation. But what if you want separate implementations for each interface?
In this case, you can create what are called explicit interface member implementations. An explicit interface member implementation has the following characteristics:
When you want to call method that has explicit interface implementations, you should casting your object to interface like shown in following code:
Or you can use
- Like all interface implementations, it is placed in the class or struct implementing the interface.
- It is declared using a qualified interface name, which consists of the interface name and member name, separated by a dot.
public
access modifier can not be used when implementing the methods.
public class person:IPrintIdentity,IBankAccount { .... void IPrintIdentity.printIdentity() { Console.WriteLine("Person's Name:" + getName()); Console.WriteLine("Age:" + getAge()); } void IBankAccount.printIdentity() { Console.WriteLine("Person's Name:" + getName()); Console.WriteLine("Age:" + getAge()); Console.WriteLine("Account Balance" + accountBalance + " USD"); } .... } public class company : IPrintIdentity,IBankAccount { .... void IPrintIdentity.printIdentity() { Console.WriteLine("Company's Name:" + getName()); Console.WriteLine("Age:" + getAge()); } void IBankAccount.printIdentity() { Console.WriteLine("Company's Name:" + getName()); Console.WriteLine("Age:" + getAge()); Console.WriteLine("Account Balance" + accountBalance + " USD"); } .... }
When you want to call method that has explicit interface implementations, you should casting your object to interface like shown in following code:
static void Main(string[] args) { person me = new person { firstName = "Yang", lastName = "Sopiana", bornDate = new date(1986, 11, 2),accountBalance=200 }; company myCompany = new company { name = "Samsung Electronics Indonesia", builtDate = new date(1990, 11, 20) ,accountBalance=1900}; ((IPrintIdentity)me).printIdentity(); //invoke IPrintIdentity.printIdentity implementation ((IBankAccount)me).printIdentity(); //invoke IBankAccount.printIdentity implementation ((IPrintIdentity)myCompany).printIdentity(); //invoke IPrintIdentity.printIdentity implementation ((IBankAccount)myCompany).printIdentity(); //invoke IBankAccount.printIdentity implementation }
Or you can use
as
keyword for casting, like following code:
static void Main(string[] args) { person me = new person { firstName = "Yang", lastName = "Sopiana", bornDate = new date(1986, 11, 2),accountBalance=200 }; company myCompany = new company { name = "Samsung Electronics Indonesia", builtDate = new date(1990, 11, 20) ,accountBalance=1900}; IPrintIdentity identity; IBankAccount bankAccount; identity = me as IPrintIdentity; bankAccount = me as IBankAccount; identity.printIdentity(); //invoke IPrintIdentity.printIdentity implementation bankAccount.printIdentity(); //invoke IBankAccount.printIdentity implementation identity = myCompany as IPrintIdentity; bankAccount = myCompany as IBankAccount; identity.printIdentity(); //invoke IPrintIdentity.printIdentity implementation bankAccount.printIdentity(); //invoke IBankAccount.printIdentity implementation Console.Read(); }
0 comments:
Post a Comment