The multiple inheritance can be implemented in C# by using the interfaces only. You can inherit a class from multiple interfaces but can inherit a class with only one class.
The below program inherit a class from two interfaces Laptop and PersonalComputer. Both the interfaces have a print method. The main function create an object of each interface and call the corresponding method.
While if we create a object of drive class then the both methods are accessible.
using System;
namespace CSharpInterview
{
interface PersonalComputer
{
void PCPrint();
}
interface Laptop
{
void LaptopPrint();
}
class Computer : Laptop, PersonalComputer
{
public void PCPrint()
{
Console.WriteLine("PC Print Method.");
}
public void LaptopPrint()
{
Console.WriteLine("Laptop Print Method.");
}
}
class Programs
{
static void Main(string[] args)
{
Laptop ObjLaptop = new Computer();
ObjLaptop.LaptopPrint();
PersonalComputer ObjPersonalComputer = new Computer();
ObjPersonalComputer.PCPrint();
Console.WriteLine("Computer Class...");
Computer ObjComputer = new Computer();
ObjComputer.LaptopPrint();
ObjComputer.PCPrint();
Console.ReadLine();
}
}
}
No comments:
Post a Comment