Interfaces Implement करना
किसी interface को implement करने का मतलब है कि कोई class promise list पर sign करती है और फिर असल में उस पर हर action प्रदान करती है। यह किसी job candidate जैसा है जो दिखाता है कि वे ज़रूरी सभी tasks कर सकते हैं।
In this page:
Syntax
class ClassName : IInterfaceName
{
public ReturnType MethodName() // every interface member needs a body
{
}
}
Implementing Interfaces
कोई class किसी interface को colon के बाद list करके और interface द्वारा declared हर member के लिए एक concrete body देकर इसे implement करती है। एक भी member implement न करना एक compile error है।
interface type के रूप में typed एक variable उसे implement करने वाली किसी भी class का object रख सकता है।
Warning:
किसी interface के सिर्फ कुछ members implement करना compile नहीं होगा — हर member ज़रूरी है।
उदाहरण: Implementing Interfaces
using System;
interface IShape
{
double Area();
double Perimeter();
}
class Square : IShape
{
public double Side;
public Square(double side) { Side = side; }
public double Area() => Side * Side;
public double Perimeter() => Side * 4;
}
class Program
{
static void Main(string[] args)
{
IShape shape = new Square(5);
Console.WriteLine(shape.Area());
Console.WriteLine(shape.Perimeter());
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- interface का हर member implement न करना, जो एक compile error है।
- किसी interface method को non-public implement करना, जिसकी implicit implementations के लिए अनुमति नहीं है।
- signature को थोड़ा बदलना, जिससे method interface से मेल नहीं खाता।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: