base Keyword क्या है
base keyword किसी child class को अपने parent से बात करने देता है, जैसे mom से पूछना कि उसने कुछ कैसे किया। यह किसी method या constructor का parent का version तक पहुंचता है।
In this page:
Syntax
class DerivedClass : BaseClass
{
public DerivedClass(type parameter) : base(parameter) // call the base constructor
{
}
public override void MethodName()
{
base.MethodName(); // call the base version
}
}
The base Keyword
base किसी derived class के अंदर से immediate parent class को refer करता है, आपको base constructor या किसी overridden base method को explicitly call करने देते हुए।
किसी constructor में base(args) initialization काम को parent को forward करता है। यह base class द्वारा पहले से handle किए logic को दोहराने से बचाता है।
Note:
किसी override के अंदर जब आप parent के behavior को replace नहीं बल्कि extend करना चाहें तब
base.MethodName() उपयोग करें।उदाहरण: The base Keyword
using System;
class Animal
{
public string Name;
public Animal(string name)
{
Name = name;
}
public virtual void Describe()
{
Console.WriteLine($"I am {Name}");
}
}
class Dog : Animal
{
public Dog(string name) : base(name) { }
public override void Describe()
{
base.Describe();
Console.WriteLine("...and I am a dog");
}
}
class Program
{
static void Main(string[] args)
{
Dog d = new Dog("Rex");
d.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. #}
आम गलतियां
- base class के पास कोई default constructor न होने पर सही arguments के साथ base constructor call करना भूल जाना।
- किसी static method में
baseउपयोग करना, जहां यह उपलब्ध नहीं है। - किसी abstract method के लिए
base.Method()call करना, जो एक compile error है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: