explicit interface implemenation
Sun Oct 12 2025 09:46:23 GMT+0000 (Coordinated Universal Time)
Saved by
@rcb
using System;
// Step 1: Base interface
interface IShape
{
void Draw();
}
// Step 2: Derived interface inherits from IShape
interface IColorShape : IShape
{
void FillColor();
}
// Step 3: Implement the derived interface explicitly
class Rectangle : IColorShape
{
// Explicit implementation of Draw() from IShape
void IShape.Draw()
{
Console.WriteLine("Drawing a Rectangle");
}
// Explicit implementation of FillColor() from IColorShape
void IColorShape.FillColor()
{
Console.WriteLine("Filling Rectangle with Blue color");
}
}
class Program
{
static void Main()
{
// Create object of Rectangle
Rectangle rect = new Rectangle();
// To access explicit interface methods, we need to cast to interface
IShape shape = rect;
shape.Draw(); // Calls IShape.Draw()
IColorShape colorShape = rect;
colorShape.FillColor(); // Calls IColorShape.FillColor()
Console.ReadLine();
}
}
content_copyCOPY
Comments