Base Class and Derived Class in c#
In C# programming, inheritance is a concept or feature in object-oriented programming (OOP) in which a derived class inherits the features and functions of a base class by inheriting the field, properties, and method behavior of a subclass. This allows C# programmers to create new subclasses or multiple derived classes based on an existing class. This increases the source code reuse concept and reduces program source code redundancy.

So, let’s get to know Base Class and Derived Class better in the C# programming.
Important Features of Base Class and Derived Class.
- Base Class (also known as parent class or superclass) – A base class in C# programming is a class that provides common functionality to share its functions and features with other classes in the program, such as derived class subclasses.
- Derived class (known as child class) – In a C# program, a derived class is a subclass that inherits the functionality, concepts, and properties of a base or root parent class, and can be extended or overridden in a derived class to add more program class functions and features.
Creating a Base Class and a Derived Class in a C# Program.
So, let’s understand the concept of inheriting properties and methods from a base class and a derived class in C# programming with a simple example.
Course (base class) and CSharpCourse (derived class) example.
using System;
//here we create course as the parent class
public class Course
{
//here we define course class fields
public string CourseName;
public int Duration;
//here we create a course class constructor
public Course(string courseName, int duration)
{
CourseName = courseName;
Duration = duration;
}
//here we define a course method
public void ShowCourse()
{
Console.WriteLine($”Course Name – {CourseName}, Duration – {Duration} months”);
}
}
// here we create a Derived class which inherits from course class
public class CSharpCourse : Course
{
// here we define a New field for C# class
public string Level;
// here we create a Constructor
public CSharpCourse(string courseName, int duration, string level)
: base(courseName, duration)
{
Level = level;
}
// here we define a Method specific to CSharpCourse class
public void ShowLevel()
{
Console.WriteLine($”Level – {Level}”);
}
// here we are hiding parent method
public new void ShowCourse()
{
Console.WriteLine($”\n Course – {CourseName} \n Duration – {Duration} months”);
}
}
// here we define a Main class
class program
{
static void Main(string[] args)
{
//here we create a Parent class object
Course course = new Course(“C# Programming Language”, 1);
Console.WriteLine(“| Course Information |”);
course.ShowCourse();
Console.WriteLine();
// here we write a Derived class object
CSharpCourse csharp = new CSharpCourse(“C# Programming”, 2, “Expert Level”);
Console.WriteLine(“| C# Course info |”);
csharp.ShowCourse();
csharp.ShowLevel();
}
}
Course (base class) and CSharpCourse (derived class) in the explanation.
- In this program, Course is the base class. It has common properties (CourseName, Duration) and a method called ShowCourse, which are shared by all Course classes.
- CSharpCourse is a derived class. It inherits properties from the Course class using the: base(courseName, duration, level) syntax. It also adds its own unique property (ShowCourse) and method (ShowLevel).
- Furthermore, it overrides the course method using the new keyword to provide a specialized implementation for Course (this hides the base class method).
Using Base and Derived Classes in C#.
So, let’s see how we can create objects of these classes and use their class methods in a C# program.
Using Base and Derived Classes Example.
class Program
{
static void Main()
{
// Here we are creating an object of the base class
Course course = new Course(“C# Programming Language”, 1);
course.ShowCourse();
// Here we are creating an object of the derived class
CSharpCourse csharp = new CSharpCourse(“C# Programming”, 2, “Advanced”);
csharp.ShowCourse();
csharp.ShowLevel();
// Here the derived class can also be referred to as a base class object
Course csharpAsCourse = new CSharpCourse(“ASP.NET with C#”, 3, “Intermediate Level”);
csharpAsCourse.ShowCourse();
}
}
Explaining Base and Derived Classes.
Creating objects of the Course class.
- Here, Course is an instance of the base class Course class. We can call its ShowCourse method.
Creating objects of the CSharpCourse class.
- Here, CSharpCourse is an instance of the derived class CSharpCourse, which contains both inherited and new methods. We can call both ShowCourse() and ShowLevel() methods specifically for it.
Using a base class reference for a derived class object.
You can assign a CSharpCourse class object to a variable of type Course. This is an example of complete polymorphism. When you call ShowCourse on a CourseAsCSharpCourse object, the CSharpCourse class method is invoked due to runtime method binding (dynamic dispatch).
Inheritance Mechanism in C# Programming.
- Fields and Properties – A derived class created in a C# program automatically inherits all the fields and properties of the base or root parent class. However, it cannot access the private class member fields of the base class.
- Methods – In a class program, a derived class can directly access the public and protected class methods of the base class. Furthermore, the derived class can override or hide the base class’s methods, a concept that incorporates polymorphism.
Method Overriding in C#.
To modify the default behavior of a class method defined in a derived class program, if the base class method is marked or indicated as virtual or abstract, you can apply the override keyword to it.
Method Overriding Example in a C# Program.
using System;
// here we define a Base class with a virtual method
public class courses
{
public string CourseName;
public Course(string courseName)
{
CourseName = courseName;
}
// here Virtual method allows us to overriding in derived classes
public virtual void ShowDetails()
{
Console.WriteLine($”{CourseName} is a programming course.”);
}
}
//here Derived class that overrides the base class method
public class CSharpCourse : Course
{
public CSharpCourse(string courseName) : base(courseName) { }
public override void ShowDetails()
{
Console.WriteLine($”{CourseName}, programming course.”);
}
}
public class program
{
static void Main()
{
Course course = new Course(“C# Programming Language”);
course.ShowDetails();
CSharpCourse csharp = new CSharpCourse(“Expert Level of C#”);
csharp.ShowDetails();
}
}
Here in this Method Overriding example.
- Here in the C# program, a class method called ShowDetails in the Course class is marked or indicated as virtual, meaning it can be overridden by a derived class.
- The CSharpCourse class overrides ShowDetails to provide a specific implementation for CSharpCourse.
Use of Base Class Constructor in Derived Classes in C#.
When you create an instance of a derived class, you can call the base class’s constructor using the base keyword to initialize the base class’s members.
Example of calling the base class constructor.
using System;
public class Course
{
public string CourseName;
// Here we create a Base class constructor
public Course(string courseName)
{
CourseName = courseName;
}
}
public class CSharpCourse : Course
{
public CSharpCourse(string courseName) : base(courseName) // Here it is calling the base class with the constructor
{
}
}
public class Program
{
static void Main()
{
CSharpCourse csharp = new CSharpCourse(“C# Programming Language”);
Console.WriteLine(csharp.CourseName); // Result is – C# Programming Language
}
}
Here’s the Base Class Constructor in Derived Classes example.
- Here, the CSharpCourse class calls the Course class’s constructor, using base(courseName) to initialize the courseName field in the base class.
Access modifier concept in class inheritance.
- Private members – If private field members are defined in a root parent or base class, they cannot be accessed by derived classes.
- Protected members – Protected class members declared in a user-defined base class are directly accessible in derived classes.
- Public and internal members – These are inherited by the derived class in subclasses, and can be accessed within the class itself if needed.
Example of protected class member access.
Using System;
public class courses
{
protected string courseName; // here we define a Protected class member
public Course(string courseName)
{
this.courseName = courseName;
}
}
public class CSharpCourse : Course
{
public CSharpCourse(string courseName) : base(courseName) { }
public void ShowCourseName()
{
Console.WriteLine($”The course name is, {courseName}.”);
}
}
public class program
{
static void Main()
{
CSharpCourse csharp = new CSharpCourse(“C# Programming”);
csharp.ShowCourseName(); // Result is – The course name is, C# Programming.
}
}
Here’s an example of protected class member access.
- Here, the courseName field in the Course class is declared protected, so the CSharpCourse class can access it directly.
Base Class and Derived Class Summary in C#.
- Base Class – Provides common functionality, such as class fields, properties, and methods, in a user-defined class. It inherits properties and methods from the derived class.
- Derived Class – A derived class inherits the properties, functions, and features of the base class, adopting the functionality of the parent or base class and further enhancing or modifying it.
- Access Modifiers – Public, protected, and private control access to base class field members declared in a C# program helps manage and control access.
- Method Overriding – Declaring a derived class in a C# program allows overriding base class methods using the override keyword.
- Constructor – In a user-defined derived class, the base class constructor can be called using the base keyword.
