C#-এ মাল্টিপল ইনহেরিটেন্স সরাসরি সাপোর্টেড নয়। একাধিক ইনহেরিটেন্স ইমপ্লিমেন্ট করতে ইন্টারফেস ব্যবহার করতে হয়।
ইন্টারফেস PaintCost ক্লাস Shape-এ:
public interface PaintCost {
int getCost(int area);
}
বেস ক্লাস Shape, ডেরাইভ্ড ক্লাস Rectangle:
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 80;
}
}
সম্পূর্ণ উদাহরণ
using System;
namespace MyInheritance {
class Shape {
public void setWidth(int w) { width = w; }
public void setHeight(int h) { height = h; }
protected int width;
protected int height;
}
public interface PaintCost {
int getCost(int area);
}
class Rectangle : Shape, PaintCost {
public int getArea() { return (width * height); }
public int getCost(int area) { return area * 80; }
}
class RectangleDemo {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(8);
Rect.setHeight(10);
area = Rect.getArea();
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}", Rect.getCost(area));
Console.ReadKey();
}
}
}
নোট: একটি ক্লাস একাধিক ইন্টারফেস ইমপ্লিমেন্ট করতে পারে, কিন্তু শুধু একটি বেস ক্লাস থেকে ইনহেরিট করতে পারে।