One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.
Base and Derived Classes
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.
Syntax
Following is the syntax used in C# for creating derived classes −
<acess-specifier> class <base_class> {
...
}
class <derived_class> : <base_class> {
...
}
Advertisement
Example of Inheritance
In this example, we create an inheritance by considering a base class 'Shape' and its derived class 'Rectangle' −
using System;
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape {
public int getArea() {
return (width * height);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result −
Total area: 35
Initializing Base Class
When the derived class inherits from a base class, we can initialize the base class using the base keyword in the constructor of the derived class.
The derived class inherits the base class member variables and member methods. Thus, the base class object must be created before the derived class object. We can specify the base class initialization in the member initialization list.
Syntax
Let's see the initialization syntax of the base class −
class BaseClass {
public BaseClass(int value) {
Console.WriteLine("Base class constructor called with value: " + value);
}
}
class DerivedClass : BaseClass {
public DerivedClass(int value) : base(value) {
Console.WriteLine("Derived class constructor called.");
}
}
Example
The following program demonstrates. How we can initialize the base class member variable in the derived class −
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
protected double length;
protected double width;
public Rectangle(double l, double w) {
length = l;
width = w;
}
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class Tabletop : Rectangle {
private double cost;
// initializing base class member variable
public Tabletop(double l, double w) : base(l, w) { }
public double GetCost() {
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display() {
base.Display();
Console.WriteLine("Cost: {0}", GetCost());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
Single Inheritance
Single inheritance is a basic type of inheritance where a class inherits from another single class.
Example
The following example demonstrates single inheritance:

Join the conversation! Your thoughts help the community grow.