Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

What is inheritance in C#?

Sheza Munir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

C# is an object-oriented language that supports inheritance. It supports single class inheritance, but not multiple class inheritance.

Inheritance

Inheritance offers reusability. This means that the functions and variables already declared in one class can be reused in another class by making it a child of that class. This prevents the need for redundant code in the new class that is declared.

Keywords

Inheritance involves the use of base and derived classes.

  • Base class: This is the class from which the functions and variables are inherited. This is the parent class, also known as the super class.
  • Derived class: This is the class that inherits functions and variables from the super class. This is the child class, also known as the sub-class.

Overriding inherited class

If both the base class and the derived class have a function of the same name, the method associated with the derived class will be executed and the inherited function will be over-ridden. For this, the derived class function must have the same name, same parameters, and same return type (or sub-type) as a method in its base class.

Code

The following code snippet shows how inheritance works in C#:

// Inheritance in C#
using System;
namespace InheritanceApplication {
// this is the base class
public class Educative {
// constructor of base class
public Educative() {
Console.WriteLine("Base class constructor");
}
// method of the base class
public int base_adder(int x, int y) {
Console.WriteLine("Using method of base class:");
int sum = x + y;
return sum;
}
}
// this is the derived class
public class Edpresso: Educative {
// constructor of derived class
public Edpresso() {
Console.WriteLine("Derived class constructor");
}
}
class Tester {
static void Main(string[] args) {
// creating object of base class
Educative Edu = new Educative();
// creating object of derived class
Edpresso Edp = new Edpresso();
// calling the method of super class using sub class object
int added = Edp.base_adder(2,3);
Console.WriteLine("sum = {0}", added);
}
}
}

Constructors

If you look at the output of the code snippet above, you will notice the following:

Base class constructor
Base class constructor
Derived class constructor

A constructor is a class method that is accessed automatically when an instance of a class is declared. Here, we declare an instance of the base class, and “Base class constructor” is printed.

When we declare an instance of the derived class, as it is inheriting from the base class, it will first access the base class constructor, then its own constructor. This leads to the output:
Base class constructor
Derived class constructor

RELATED TAGS

c#

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring