← Back to C# Course | Chapter 7: Inheritance | Lesson 4 of 6

Abstract Classes क्या हैं

एक abstract class एक अधूरा blueprint है जिसे आप सीधे नहीं बना सकते। यह नियम सेट करता है और कुछ हिस्से इससे inherit करने वाली classes के भरने के लिए छोड़ता है।

In this page:

  1. Abstract Classes
Syntax
csharp
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

markup
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();
    }
}
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. #}
आम गलतियां
  1. new से किसी abstract class का object बनाने की कोशिश करना, जो एक compile error है।
  2. derived class में हर abstract member implement न करना, जो एक compile error है जब तक class भी abstract न हो।
  3. किसी abstract method को एक body देना, जिसकी अनुमति नहीं है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.