We use base and derived classes to organize code by sharing common features and adding new ones. It helps avoid repeating code and makes programs easier to understand.
Base class and derived class in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
class BaseClass { // common properties and methods } class DerivedClass : BaseClass { // additional properties and methods }
The base class is the parent that holds shared code.
The derived class inherits from the base and can add or change features.
Examples
C Sharp (C#)
class Animal { public void Eat() { Console.WriteLine("Eating food"); } } class Dog : Animal { public void Bark() { Console.WriteLine("Barking"); } }
C Sharp (C#)
class Vehicle { public void Start() { Console.WriteLine("Starting vehicle"); } } class Car : Vehicle { public void Honk() { Console.WriteLine("Honking horn"); } }
Sample Program
This program shows a Dog class inheriting from Animal. Dog can use Eat() from Animal and also Bark() on its own.
C Sharp (C#)
using System; class Animal { public void Eat() { Console.WriteLine("Animal is eating"); } } class Dog : Animal { public void Bark() { Console.WriteLine("Dog is barking"); } } class Program { static void Main() { Dog myDog = new Dog(); myDog.Eat(); // inherited from Animal myDog.Bark(); // own method } }
Important Notes
Derived classes get all public and protected members from the base class automatically.
You can create many derived classes from one base class to share common code.
Use inheritance to keep code clean and avoid repeating the same code in multiple places.
Summary
Base class holds shared code for other classes.
Derived class inherits from base and can add or change features.
This helps organize code and reuse common parts easily.
Practice
1. What is the main purpose of a
base class in C#?easy
Solution
Step 1: Understand the role of base class
A base class contains common code that multiple classes can share to avoid repetition.Step 2: Compare options with this role
To hold common code that other classes can reuse matches this purpose exactly, while others describe incorrect or unrelated uses.Final Answer:
To hold common code that other classes can reuse -> Option AQuick Check:
Base class = shared code [OK]
Hint: Base class shares code for reuse [OK]
Common Mistakes:
- Thinking base class cannot be instantiated
- Confusing base class with interface
- Believing base class only stores data
2. Which of the following is the correct syntax to declare a derived class
Car that inherits from a base class Vehicle in C#?easy
Solution
Step 1: Recall C# inheritance syntax
In C#, a derived class uses a colon (:) followed by the base class name.Step 2: Match options with correct syntax
class Car : Vehicle { } uses the correct syntax:class Car : Vehicle { }. Others use incorrect keywords or symbols.Final Answer:
class Car : Vehicle { } -> Option BQuick Check:
Inheritance syntax = colon (:) [OK]
Hint: Use colon (:) to inherit in C# [OK]
Common Mistakes:
- Using 'inherits' instead of ':'
- Using 'extends' like in Java
- Using arrow '->' symbol
3. What will be the output of this C# code?
class Animal {
public void Speak() {
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal {
public void Speak() {
Console.WriteLine("Dog barks");
}
}
class Program {
static void Main() {
Animal a = new Dog();
a.Speak();
}
}medium
Solution
Step 1: Understand method hiding vs overriding
TheSpeakmethod inDoghides the base method but is not markedvirtualoroverride.Step 2: Check method call behavior
Sinceais of typeAnimal, it callsAnimal.Speak()ignoringDog's method.Final Answer:
Animal speaks -> Option CQuick Check:
Non-virtual method call = base method [OK]
Hint: Non-virtual methods call base version [OK]
Common Mistakes:
- Assuming derived method runs without override
- Confusing method hiding with overriding
- Expecting polymorphism without virtual keyword
4. Identify the error in this code snippet:
class Person {
public string Name;
}
class Student : Person {
public string Name;
}
class Program {
static void Main() {
Student s = new Student();
s.Name = "Alice";
Console.WriteLine(s.Name);
}
}medium
Solution
Step 1: Analyze the code execution
The code declares a public fieldNamein bothPersonandStudent, causing the derived field to hide the base one. This is allowed in C#.Step 2: Determine if there is an error
The code compiles (with a compiler warning about hiding), executes successfully, and prints 'Alice' as it accesses the derived class'sNamefield.Final Answer:
No error, code runs and prints 'Alice' -> Option AQuick Check:
Field hiding allowed, no hard error [OK]
Hint: Field hiding is allowed but generates a warning [OK]
Common Mistakes:
- Thinking derived class cannot declare same field name
- Believing missing constructor causes issue
- Mistaking fields for methods that need override
5. You want to create a base class
Shape with a method Area() that derived classes Circle and Rectangle must implement differently. Which approach is best in C#?hard
Solution
Step 1: Understand requirement for method implementation in derived classes
The base classShapeshould force derived classes to provide their ownArea()implementation.Step 2: Choose correct C# feature
DeclaringArea()as abstract inShaperequires derived classes to implement it, matching the requirement.Final Answer:
Declare Area() as an abstract method in Shape and implement in derived classes -> Option DQuick Check:
Abstract method = must implement in derived [OK]
Hint: Use abstract method to force implementation [OK]
Common Mistakes:
- Using virtual without override
- Not declaring method in base class
- Confusing interface with abstract class
