By SriRam Balasubramanian
Here is an example which helps you to know how an abstract class and multiple interface methods are called, when they have conflicting names. For this example, I've used 2 interfaces and one abstract class which have only function declaration with same name. The abstract class and interfaces are inherited in the derived class for implementation. Before you go thru the code, I would suggest you to do it part by part. Simply follow the steps, you should be able to understand the implementation.
STEPS:
1) First declare 2 interfaces IInterface1, IInterface2 and an abstract class MyAbstractClass below the namespace like this.
// Interface declaration for IInterface1
public interface IInterface1
{
void xyz();
}
// Interface declaration for IInterface2
public interface IInterface2
{
void xyz();
}
// Abstract class declaration for class MyAbstractClass
public abstract class MyAbstractClass
{
public abstract void xyz();
}
2) Create a derived class with the name "Derived" and inherit the abstract class and interfaces.
class Derived : MyAbstractClass,IInterface1,IInterface2
3) Provide function definition for the inherited abstract class and interface members in the derived class as,
// Method xyz() of IInterface1
void IInterface1.xyz()
{
Console.WriteLine("xyz() method for IInterface1 called. \n");
}
// Method xyz() of IInterface2
void IInterface2.xyz()
{
Console.WriteLine("xyz() method for IInterface2 called. \n");
}
// Method xyz() of Abstract class
public override void xyz()
{
Console.WriteLine("xyz() method for abstract class called. \n");
}
4) Define the entry point for the class "Derived" and instantiate it. As well as declare interface variables and assign the "Derived" class object to the interfaces as the one given below.
private static void Main(string[] args)
{
Derived derived = new Derived();
IInterface1 interface1 = new Derived();
IInterface2 interface2 = new Derived();
5) Now with the object of the derived class and the variables of interfaces, you should be able to access the member functions of abstract class and interfaces.
derived.xyz();
interface1.xyz();
interface2.xyz();
Console.WriteLine("Press ENTER key to exit..");
Console.ReadLine();
}
6) When you compile the code, the output should look like this in the console window.
xyz() method for abstract class called.
xyz() method for IInterface1 called.
xyz() method for IInterface2 called.
Press ENTER key to exit..
The full source code is given below for your better understanding.
using System;
namespace Example
{
// Interface declaration for IInterface1
public interface IInterface1
{
void xyz();
}
// Interface declaration for IInterface2
public interface IInterface2
{
void xyz();
}
// Abstract class declaration for class MyAbstractClass
public abstract class MyAbstractClass
{
public abstract void xyz();
}
// class Derived which derives abstract class and interfaces
class Derived : MyAbstractClass,IInterface1,IInterface2
{
// Main function
[STAThread]
private static void Main(string[] args)
{
Derived derived = new Derived();
IInterface1 interface1 = new Derived();
IInterface2 interface2 = new Derived();
derived.xyz();
interface1.xyz();
interface2.xyz();
Console.WriteLine("Press ENTER key to exit..");
Console.ReadLine();
}
// Method xyz() of IInterface1
void IInterface1.xyz()
{
Console.WriteLine("xyz() method for IInterface1 called. \n");
}
// Method xyz() of IInterface2
void IInterface2.xyz()
{
Console.WriteLine("xyz() method for IInterface2 called. \n");
}
// Method xyz() of Abstract class
public override void xyz()
{
Console.WriteLine("xyz() method for abstract class called. \n");
}
}
}
Thus a user is able to access both interface and abstract class methods when they have same names. This question is frequently asked in all the .NET technical interviews.
Regards,
SriRam Balasubramanian