Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax
3.
Types of Inheritance
3.1.
Single Inheritance
3.2.
Multilevel inheritance
3.3.
Hierarchical Inheritance
4.
Sealed Keyword
5.
Important Points
6.
Advantages of Inheritance
7.
FAQs
7.1.
How do we implement reusability in inheritance?
7.2.
In what sequence the constructors of classes are called in the case of multilevel inheritance?
7.3.
Why can't we implement multiple inheritance in C#?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Inheritance in C#

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C# is an object-oriented programming language that supports inheritance. Inheritance is the ability to inherit the features and functionalities from another class. It supports the reuse of the same code. Inheritance is the most important feature of object-oriented programming. It allows the programmer to define a class in terms of another class. The class which inherits the methods (member functions) and fields (functionality) from another class is called the derived class or subclass, and the class from which the derived class acquires is called the parent class or superclass.

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Syntax

class derived_class : base_class
{
	// Methods and fields
}


Example

using System;
namespace ConsoleApplication1 {
  
// Base class
class info {
  
  // data members
    public string name;
    public int rollNo;
  
    // public method of base class 
    public void enter(string name, int rollNo)
    {
        this.name = name;
        this.rollNo = rollNo;
        Console.WriteLine("Name : " + name); 
        Console.WriteLine("Roll No : " + rollNo);
    }
}

// Derived class  
// inheriting the info class
class Student : info {
  
    // constructor
    public Student()
    {
        Console.WriteLine("Student Details");
    }
}
  
class Example {
  
    static void Main(string[] args)
    {
        // creating object of derived class
        Student student1 = new Student();
        student1.enter("John Roger", 21);
    }
}
}


Output

Student Details
Name : John Roger
Roll No : 21
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Inheritance

Single Inheritance

Single inheritance is defined as the type of inheritance in which a derived class/subclass inherits the methods and fields of only one base class/superclass.

Example

using System;  
  public class oops  
    {  
        public oops() { Console.WriteLine("It is an object-oriented programming language"); }  
  }  
  public class Csharp: oops  
  {  
        public Csharp() { Console.WriteLine("We are coding in Csharp"); }  
  }  
  class SingleInheritance{  
      public static void Main(string[] args)  
        {  
            Csharp d1 = new Csharp();  
        }  
    }  


Output

It is an object-oriented programming language
We are coding in Csharp

Also see, c# interview questions

Multilevel inheritance

Multilevel inheritance is defined as the type of inheritance in which a class is derived from a class which itself is derived from another class. For example, class z is derived from class y, and class y is derived from class x.

Example

using System;  
    
  public class oops  
    {  
        public oops() { Console.WriteLine("It is an object-oriented programming language"); }  
  }  
  public class inheritance  : oops
    {  
        public inheritance() { Console.WriteLine("It supports inheritance"); }  
  }  
  public class Csharp: inheritance  
  {  
        public Csharp() { Console.WriteLine("We are coding in Csharp"); }  
  }  
  class MultilevelInheritance{  
      public static void Main(string[] args)  
        {  
            Csharp d1 = new Csharp();  
        }  
    }  


Output

It is an object-oriented programming language
It supports inheritance
We are coding in Csharp


Hierarchical Inheritance

Hierarchial inheritance is the type of inheritance in which more than one class is derived from a single base class. For example, classes B, C, D are derived from a single base class A.


Example

using System;  
    
  public class X  
    {  
        public int x=10,y=21;
  }  
  public class Y  : X
    {  
        public void add(){
            int sum=this.x + this.y;
            Console.WriteLine("Sum of "+this.x+" and "+this.y +" : "+ sum);
        }  
  }  
  public class Z: X  
  {  
        public void prod(){
        int prod=this.x * this.y;
        Console.WriteLine("Product of "+this.x+" and "+this.y +" : "+ prod);
        }    
  }  
  class HierarchicalInheritance{  
      public static void Main(string[] args)  
        {  
            Y d1 = new Y();  
            d1.add();
            Z d2 = new Z();  
            d2.prod();
        }  
    }  


Output

Sum of 10 and 21 : 31
Product of 10 and 21 : 210


Sealed Keyword

C# is an object-oriented programming language that allows inheritance. But if we don't want a class to be inherited by other classes we use the keyword “sealed” as a prefix during class declaration.

Syntax

sealed class className
{
	// Methods and Fields
}


Example

using System;  
  sealed public class oops  
    {  
        public oops() { Console.WriteLine("It is an object-oriented programming language"); }  
  }  
  public class Csharp: oops  
  {  
        public Csharp() { Console.WriteLine("We are coding in Csharp"); }  
  }  
  class useOfsealed{  
      public static void Main(string[] args)  
        {  
            Csharp d1 = new Csharp();  
        }  
    }


Output

'Csharp': cannot derive from sealed type 'oops'

Important Points

  • A superclass, also known as a base class, can have multiple subclasses or derived classes, but a subclass can have only one superclass because C# doesn’t support multiple inheritance with classes.
     
  • Every class has only one direct superclass, in the absence of any other explicitly superclass, every class is implicitly a subclass of the object class.
     
  • A derived class does not inherit the private members of its base/parent class. If the superclass has properties for accessing its private fields only, then a subclass can inherit the private members of the base class.
     
  • A derived class inherits all the methods and fields of its base class. Derived classes do not inherit constructors, tho the constructor of a base class can be invoked from the derived class.

Advantages of Inheritance

  • By using inheritance, we can reduce redundancy.
  • Using inheritance, we can reuse the code already written and promote reusability.
  • It makes the code more readable by reducing the size of the source code.
  • It makes the code more structured and easy to manage.
  • It supports code extensibility by overriding the base class functionality within the child class.

 

Know What is Object in OOPs here in detail.

FAQs

How do we implement reusability in inheritance?

Inheritance supports reusability. In inheritance, a class is derived from another class. The derived class inherits the methods and the fields of the base class, hence implementing reusability that is, we reuse the fields and methods of an existing class. 

In what sequence the constructors of classes are called in the case of multilevel inheritance?

The constructors of inherited classes are called in the same order in which they are inherited in the case of multilevel inheritance,i.e., starting from the first base class to the last derived class.

Why can't we implement multiple inheritance in C#?

C# does not support multiple inheritance as in C#, a derived class can have only one base class.

Conclusion

In this article, we have extensively discussed what inheritance is, how we can implement it, and the different types of inheritance supported in the C# programming language with examples and their implementation in Visual Studio Code.

We hope that this blog has helped you enhance your knowledge regarding inheritance in the C# programming language and if you would like to learn more, check out our articles on understanding the difference between C# and C++, what is Multiple Inheritance in Java, what is hybrid inheritance in Java.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Previous article
Static Class in C#
Next article
C# Polymorphism
Live masterclass