Hello Friends,
Below program describes, how to implement multiple interfaces in C#. Specially when two interfaces having same method name, then how to resolve the conflict in multiple implementation, this is shown in below code.
Please also mark that C# and JAVA don't support multiple inheritance, but support multiple implementation. We inherit the classes but implement the interfaces. This particular word "Inherit" should not be used in case of implementing the Interfaces.
Feel free to comment:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleMultipleInheritance
{
interface InterfaceA
{
void methodA();
void methodB();
void methodC();
}
interface InterfaceB
{
void methodC();
void methodD();
void methodE();
}
abstract class absA
{
//Note: 1. Virtual method can be used with abstract
// as well as simple class.
// as well as simple class.
//2. Virtual methods don't need to be override, its upto the
programmar.
//3. Virtual Methods need to have body, means they need to
// have definition.
//3. Virtual Methods need to have body, means they need to
// have definition.
public virtual void methodG()
{
}
//Note: 1. Abstract method can be used only with abstract
class
//2. Abstract methods need to be override, its hard and fast
rule.
//3. Abstract methods can not have body. They should
//be just created without definition inside abstract class only.
//Note: If an abstract class will have only abstract members e.g.
//abstract methods, properties,
//then I guess that will work as Interface. Do some googling
//on Interface vs fully abstract class
//3. Abstract methods can not have body. They should
//be just created without definition inside abstract class only.
//Note: If an abstract class will have only abstract members e.g.
//abstract methods, properties,
//then I guess that will work as Interface. Do some googling
//on Interface vs fully abstract class
public abstract void methodC();
}
class drvdA : absA, InterfaceA,
InterfaceB
{
public void methodA()
{
Console.WriteLine("method
A");
}
public void methodB()
{
Console.WriteLine("method
B");
}
public void methodD()
{
Console.WriteLine("method
D");
}
public void methodE()
{
Console.WriteLine("method
E");
}
public void methodF()
{
Console.WriteLine("method
F");
}
// We don't need to override the virtual function.
// Its upto the programmar
// Its upto the programmar
// public override void methodG()
//{
//
Console.WriteLine("method G");
//
//base.methodG();
//}
public override void methodC()
{
Console.WriteLine("Abstract
Class method C");
//base.methodG();
}
// This is how we call the common methods of Interfaces
// which we implemented in our class. We use fully qualified name.
// which we implemented in our class. We use fully qualified name.
void InterfaceB.methodC()
{
Console.WriteLine("InterfaceB
method C");
}
void InterfaceA.methodC()
{
Console.WriteLine("InterfaceA
method C");
}
}
//Empty class inheriting another class which is not empty
class drvdB : drvdA
{ }
class Program
{
static void Main(string[] args)
{
//Execution Time Calculation
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Console.WriteLine("ABC");
sw.Stop();
Console.WriteLine(sw.Elapsed );
sw.Start();
Console.WriteLine("ABC");
sw.Stop();
Console.WriteLine(sw.Elapsed);
//making instance of derived empty class
drvdB b = new drvdB();
//Able to get the methods of base class which are not
implemented
// into derived class
// into derived class
b.methodA();
drvdA a = new drvdA();
a.methodA();
a.methodB();
a.methodC();
a.methodD();
a.methodE();
a.methodF();
a.methodG();
//Calling those methods which are implemented from
Interfaces,
// through the instance of its derived class...
// through the instance of its derived class...
((InterfaceA)a).methodC();
((InterfaceB)a).methodC();
Console.Read();
//ConstructorStrategy cs = new ConstructorStrategy();
}
}
}
No comments:
Post a Comment