0
0
C Sharp (C#)programming~10 mins

Base class and derived class in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Base class and derived class
Define Base Class
Define Derived Class : Base Class
Create Derived Class Object
Access Base and Derived Members
Use Derived Object with Base Class Features
First, we define a base class. Then, we create a derived class that inherits from the base. We create an object of the derived class and access both base and derived members.
Execution Sample
C Sharp (C#)
using System;

class Animal {
  public void Eat() {
    Console.WriteLine("Eating");
  }
}

class Dog : Animal {
  public void Bark() {
    Console.WriteLine("Barking");
  }
}

class Program {
  static void Main() {
    var dog = new Dog();
    dog.Eat();
    dog.Bark();
  }
}
This code defines a base class Animal with method Eat, a derived class Dog with method Bark, then creates a Dog object and calls both methods.
Execution Table
StepActionObjectMethod CalledOutput
1Create Dog objectdogN/AN/A
2Call Eat methoddogEat()Eating
3Call Bark methoddogBark()Barking
4End of programN/AN/AN/A
💡 Program ends after calling both base and derived class methods on dog object.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
dognullDog object createdDog object unchangedDog object unchangedDog object unchanged
Key Moments - 2 Insights
Why can the dog object call the Eat() method even though Eat() is defined in Animal, not Dog?
Because Dog inherits from Animal, it gets all Animal's public methods. See execution_table step 2 where dog calls Eat() successfully.
What happens if we try to call Bark() on an Animal object?
Animal does not have Bark() method, so calling Bark() on Animal object causes a compile error. Only Dog objects have Bark().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when dog.Eat() is called at step 2?
ABarking
BEating
CError
DNo output
💡 Hint
Check the Output column at step 2 in execution_table.
At which step is the Dog object created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Action column in execution_table for object creation.
If we remove inheritance (Dog no longer inherits Animal), what happens when calling dog.Eat()?
AIt works as before
BRuns but no output
CCompile error: Eat() not found
DRuntime error
💡 Hint
Inheritance allows Dog to use Animal's methods; without it, Eat() is unknown.
Concept Snapshot
Base class defines common features.
Derived class inherits base features and adds new ones.
Create derived object to use both base and derived methods.
Inheritance enables code reuse and organization.
Derived class syntax: class Derived : Base {}
Full Transcript
In C#, a base class is a class that provides common methods or properties. A derived class inherits from the base class, gaining its features and adding new ones. In the example, Animal is the base class with method Eat. Dog is the derived class inheriting Animal and adding Bark. When we create a Dog object, it can call both Eat (from Animal) and Bark (from Dog). This shows how inheritance works: derived classes reuse base class code. If Dog did not inherit Animal, calling Eat on Dog would cause an error. This concept helps organize code and avoid repetition.