C# > Object-Oriented Programming (OOP) > Inheritance > Using base Keyword
Demonstrating Inheritance and the 'base' Keyword in C#
This code snippet demonstrates the use of inheritance and the 'base' keyword in C#. Inheritance allows a class to inherit properties and methods from a parent class. The 'base' keyword allows a derived class to access members of its base class, particularly when overriding methods or constructors.
Code Example
This example defines two classes: `Animal` and `Dog`. The `Dog` class inherits from the `Animal` class. The `Dog` constructor uses `base(name)` to call the `Animal` constructor, passing the name. The `MakeSound` method in `Dog` overrides the `MakeSound` method in `Animal`. It then calls the `Animal`'s `MakeSound` method using `base.MakeSound()` before adding its own sound. This ensures that both the generic animal sound and the dog's bark are printed.
using System;
public class Animal
{
public string Name { get; set; }
public Animal(string name)
{
Name = name;
Console.WriteLine("Animal constructor called with name: " + name);
}
public virtual void MakeSound()
{
Console.WriteLine("Generic animal sound");
}
}
public class Dog : Animal
{
public string Breed { get; set; }
public Dog(string name, string breed) : base(name)
{
Breed = breed;
Console.WriteLine("Dog constructor called with name: " + name + " and breed: " + breed);
}
public override void MakeSound()
{
base.MakeSound(); // Call the base class's MakeSound method
Console.WriteLine("Woof!");
}
}
public class Example
{
public static void Main(string[] args)
{
Dog myDog = new Dog("Buddy", "Golden Retriever");
myDog.MakeSound();
}
}
Concepts Behind the Snippet
Inheritance: Inheritance is a fundamental concept in OOP that allows a class (derived or child class) to inherit properties and methods from another class (base or parent class). This promotes code reuse and establishes an 'is-a' relationship (e.g., a Dog is an Animal).
'base' Keyword: The 'base' keyword is used to access members of the base class from within a derived class. It is particularly useful in constructors and when overriding methods. Using 'base' allows you to extend or modify the behavior of the base class without completely rewriting it.
Real-Life Use Case
Consider a software application for managing different types of vehicles. You might have a base class called 'Vehicle' with properties like 'NumberOfWheels' and methods like 'StartEngine()'. Derived classes like 'Car', 'Truck', and 'Motorcycle' can inherit from 'Vehicle' and add their specific properties (e.g., 'NumberOfDoors' for 'Car', 'CargoCapacity' for 'Truck') and override methods (e.g., 'StartEngine()' to include specific starting procedures for each type of vehicle). Using 'base' in this scenario allows the derived classes to reuse the common 'Vehicle' functionality while adding their own specific behavior.
Best Practices
Interview Tip
Be prepared to explain the difference between inheritance and composition, as well as the purpose of the 'base' keyword. You might be asked to provide examples of when to use inheritance and how 'base' can be used to access members of the base class. A good understanding of these concepts is crucial for object-oriented design and development.
When to Use 'base'
You should use the 'base' keyword in the following situations:
Alternatives
While inheritance is a common way to achieve code reuse, other alternatives exist:
Pros of Using 'base'
Cons of Using 'base'
FAQ
-
What happens if I don't call 'base' in the derived class constructor?
If you don't explicitly call the base class constructor using 'base', the compiler will automatically call the default (parameterless) constructor of the base class. If the base class does not have a default constructor, you will get a compilation error. -
Can I call 'base' multiple times in a method?
No, you can only call 'base' once within a single method to access the base class's implementation of the same method. Subsequent calls to 'base' within the same method are not allowed. -
Is it mandatory to call the base class constructor from the derived class constructor?
No, it's not strictly mandatory. If the base class has a default (parameterless) constructor, the compiler will call it implicitly. However, if the base class only has parameterized constructors, you *must* explicitly call one of them using 'base'.