Interface की Basics
एक interface promises की एक list है, जैसे 'जो भी उड़ सके उसके पास एक fly action होना चाहिए'। यह बताता है कि क्या करना है लेकिन कैसे नहीं।
In this page:
Syntax
interface IInterfaceName
{
ReturnType MethodName();
}
class ClassName : IInterfaceName
{
public ReturnType MethodName() { }
}
Interface Basics
एक interface members का एक contract declare करता है जो implementing classes को प्रदान करना चाहिए, लेकिन अपनी कोई implementation नहीं देता (default methods के अलावा)।
यह interface से declared होता है और conventionally I से शुरू होने वाला नाम रखता है, जैसे IShape। Interfaces define करते हैं कि कोई type *क्या* कर सकती है, *कैसे* नहीं।
Note:
Interface members implicitly public हैं — आप उन पर कभी कोई access modifier नहीं लिखते।
उदाहरण: Interface Basics
using System;
interface IGreeter
{
void Greet();
}
class EnglishGreeter : IGreeter
{
public void Greet()
{
Console.WriteLine("Hello!");
}
}
class Program
{
static void Main(string[] args)
{
IGreeter g = new EnglishGreeter();
g.Greet();
}
}
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 से सीधे एक object बनाने की कोशिश करना, जो एक compile error है।
- किसी interface में fields जोड़ना, जिसकी अनुमति नहीं है।
- यह भूल जाना कि interface members default रूप से public हैं और ज़रूरत न होने पर एक access modifier जोड़ना।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: