Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Example for Single Inheritance
Single inheritance in C# allows a derived class to inherit from only one base class. This is the most common form of inheritance where a child class extends the functionality of its parent class by inheriting all accessible members and adding its own specific methods or properties.
Syntax
Following is the syntax for single inheritance in C# −
class BaseClass {
// base class members
}
class DerivedClass : BaseClass {
// derived class members
// inherits all accessible members from BaseClass
}
Basic Single Inheritance Example
In this example, the Father class serves as the base class, and the Son class inherits from it −
using System;
class Father {
public void Display() {
Console.WriteLine("Display method from Father class");
}
}
class Son : Father {
public void DisplayOne() {
Console.WriteLine("DisplayOne method from Son class");
}
}
class Demo {
static void Main(string[] args) {
// Father class instance
Father f = new Father();
f.Display();
// Son class instance
Son s = new Son();
s.Display(); // inherited from Father
s.DisplayOne(); // own method
}
}
The output of the above code is −
Display method from Father class Display method from Father class DisplayOne method from Son class
Single Inheritance with Fields and Properties
This example demonstrates how derived classes inherit fields and properties along with methods −
using System;
class Vehicle {
protected string brand;
protected int year;
public Vehicle(string brand, int year) {
this.brand = brand;
this.year = year;
}
public void StartEngine() {
Console.WriteLine($"{brand} engine started!");
}
public void DisplayInfo() {
Console.WriteLine($"Brand: {brand}, Year: {year}");
}
}
class Car : Vehicle {
private int doors;
public Car(string brand, int year, int doors) : base(brand, year) {
this.doors = doors;
}
public void ShowCarDetails() {
Console.WriteLine($"Car Details - Brand: {brand}, Year: {year}, Doors: {doors}");
}
}
class Program {
static void Main(string[] args) {
Car myCar = new Car("Toyota", 2022, 4);
// Using inherited methods
myCar.StartEngine();
myCar.DisplayInfo();
// Using own method
myCar.ShowCarDetails();
}
}
The output of the above code is −
Toyota engine started! Brand: Toyota, Year: 2022 Car Details - Brand: Toyota, Year: 2022, Doors: 4
Key Features of Single Inheritance
-
A derived class inherits all public and protected members from the base class.
-
Private members are not directly accessible but can be accessed through public/protected methods.
-
The derived class can add its own methods and properties.
-
Constructor chaining using
base()keyword allows calling the base class constructor.
Conclusion
Single inheritance in C# enables a derived class to inherit from exactly one base class, promoting code reusability and establishing an "is-a" relationship. The derived class gains access to all accessible members of the base class while being able to add its own specific functionality.
