Abstract Classes में Abstract Methods होते हैं, जिन्हें Derived Class द्वारा Implement किया जाता है। Derived Classes में अधिक Specialized Functionality होती है।
निम्न C# में Abstract Classes के उपयोग को दर्शाने वाला एक Example है।
Example
using System; namespace Demo { abstract class Shape { public abstract int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle(int a = 0, int b = 0) { length = a; width = b; Console.WriteLine("Length of Rectangle: " + length); Console.WriteLine("Width of Rectangle: " + width); } public override int area() { return (width * length); } } class RectangleTester { static void Main(string[] args) { Rectangle r = new Rectangle(14, 8); double a = r.area(); Console.WriteLine("Area: {0}", a); Console.ReadKey(); } } }Output
Length of Rectangle: 14 Width of Rectangle: 8 Area: 112
हमारा Abstract Class ऊपर है:
abstract class Shape { public abstract int area(); }Abstract Classes के बारे में निम्न Rules हैं।
- आप Abstract Class का Instance Create नहीं कर सकते
- आप Abstract Class के बाहर Abstract Method Declare नहीं कर सकते
- जब Class को Sealed Declare किया जाता है, तो इसे Inherit नहीं किया जा सकता, Abstract Classes को Sealed Declare नहीं किया जा सकता।