Microsoft.NET 2.0 Question:
Download Job Interview Questions and Answers PDF
How to create multiple inheritance inc#, with example?
Answer:
A class can implement multiple interfaces.
public class A : Ix, Iy,Iz
This way you can achieve multiple inheritence
In C#, a class cannot have multiple inheritance with classes. But class can have multiple inheritance with one class and more than one interface.
Eg:
namespace TestClass
{
public class A
{
public void testA()
{
Console.WriteLine("In Class A");
}
}
interface IB
{
void testIB();
}
interface IC
{
void testIC();
}
public class Test : A, IB, IC
{
public void testIB()
{
Console.WriteLine("In Interface IB");
}
public void testIC()
{
Console.WriteLine("In Interface IC");
}
}
class Program
{
static void Main(string[] args)
{
Test tt = new Test();
tt.testA();
tt.testIB();
tt.testIC();
Console.ReadLine();
}
}
}
public class A : Ix, Iy,Iz
This way you can achieve multiple inheritence
In C#, a class cannot have multiple inheritance with classes. But class can have multiple inheritance with one class and more than one interface.
Eg:
namespace TestClass
{
public class A
{
public void testA()
{
Console.WriteLine("In Class A");
}
}
interface IB
{
void testIB();
}
interface IC
{
void testIC();
}
public class Test : A, IB, IC
{
public void testIB()
{
Console.WriteLine("In Interface IB");
}
public void testIC()
{
Console.WriteLine("In Interface IC");
}
}
class Program
{
static void Main(string[] args)
{
Test tt = new Test();
tt.testA();
tt.testIB();
tt.testIC();
Console.ReadLine();
}
}
}
Download Microsoft.NET 2.0 Interview Questions And Answers
PDF