Abstract Classes क्या हैं
एक abstract class एक अधूरा blueprint है जिसे आप सीधे नहीं बना सकते। यह नियम सेट करता है और कुछ हिस्से इससे inherit करने वाली classes के भरने के लिए छोड़ता है।
In this page:
Syntax
abstract class ClassName
{
public abstract ReturnType MethodName(); // no body
}
class DerivedClass : ClassName
{
public override ReturnType MethodName() { }
}
Abstract Classes
एक abstract class को सीधे instantiate नहीं किया जा सकता और यह abstract methods declare कर सकती है जिनका कोई body नहीं — derived classes को इन्हें implement करना *ज़रूरी* है।
Abstract classes एक shared concept को model करती हैं जो सिर्फ अपने concrete subtypes के ज़रिए मायने रखता है, जैसे कोई specific area formula के बिना Shape। वे अब भी abstract वालों के साथ-साथ regular, पूरी तरह-implemented methods रख सकती हैं।
Warning:
किसी abstract
Shape class पर new Shape() लिखने की कोशिश करना एक compile error है।उदाहरण: Abstract Classes
using System;
abstract class Shape
{
public abstract double Area();
public void Describe()
{
Console.WriteLine($"Area is {Area()}");
}
}
class Square : Shape
{
public double Side;
public Square(double side)
{
Side = side;
}
public override double Area()
{
return Side * Side;
}
}
class Program
{
static void Main(string[] args)
{
Square sq = new Square(4);
sq.Describe();
}
}
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. #}
आम गलतियां
newसे किसी abstract class का object बनाने की कोशिश करना, जो एक compile error है।- derived class में हर abstract member implement न करना, जो एक compile error है जब तक class भी abstract न हो।
- किसी abstract method को एक body देना, जिसकी अनुमति नहीं है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: