The base Keyword
The base keyword lets a child class talk to its parent, like asking mom how she did something. It reaches the parent's version of a method or constructor.
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 refers to the immediate parent class from inside a derived class, letting you call the base constructor or an overridden base method explicitly.
base(args) in a constructor forwards initialization work to the parent. This avoids duplicating logic that the base class already handles.
Note:
Use
base.MethodName() inside an override when you want to extend, not replace, the parent's behavior.Example: 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. #}
Common Mistakes
- Forgetting to call the base constructor with the right arguments when the base class has no default constructor.
- Using
basein a static method, where it is not available. - Calling
base.Method()for a method that is abstract, which is a compile error.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: