OOPS Concepts Tips: Inheritance in C#

By | August 27, 2014

Inheritance is one of the key features in OOPS and C# does support the OOPs principles. Because of the principle of Inheritance you can create a new class that can extend, reuse or modify the behavior and operations of another class.
The class whose members are inherited by another class is called the base class and the class which inherits are called the derived class.
In C# all the classes are implicitly inherited from the Object Class, which is the base class and provides support to all the .Net classes and gives basic services to all classes.

Here is an example to inherit from a base class in C#:

class MyDerivedClass:MyBaseClass{}

By default in C# all classes can be inherited, but you can explicitly specify if you do not want a class to be inherited by other classes. Here is how you do it in C# using the sealed keyword.

public sealed class MyClass { } 

Also if you want a class to be used only as a base class and should not be instantiated, you can do so with the abstract key word. Here is an example:

public abstract class MyClass { }

Leave a Reply