How can i use inheritance in c#

Ask a question
2

How can i use inheritance in c#

Coin iconoffer bounty
add comment

3 Answers

2

In c#, Inheritance is a one of the primary concept of object oriented programming (OOP) and it is used to inherit the properties from one class (base) to another (child) class.

The inheritance will enable us to create a new class by inheriting the properties from other classes to reuse, extend and modify the behaviour of other class members based on our requirements.

In c# inheritance, the class whose members are inherited is called a base (parent) class and the class that inherits the members of base (parent) class is called a derived (child) class.

C# Inheritance Syntax
Following is the syntax of implementing an inheritance to define a derived class that inherit the properties of base class in c# programming language.

<access_modifier> class <base_class_name>

{

// Base class Implementation

}

<access_modifier> class <derived_class_name> : <base_class_name>

{

// Derived class implementation

}

If you observe above syntax, we are inheriting the properties of base class into child class to improve the code reusability.

Following is the simple example of implementing an inheritance in c# programming language.

public class X

{

public void GetDetails()

{

    // Method implementation

}

}

public class Y : X

{

// your class implementation

}

class Program

{

static void Main(string[] args)

{

    Y y = new Y();

    y.GetDetails();           

}

}

If you observe above example, we defined a class “X” with method called “GetDetails” and the class “Y” is inheriting from class “X”. After that, we are calling a “GetDetails” method by using an instance of derived class “Y”.

In c#, it’s not possible to inherit the base class constructors in derived class and the accessibility of other members of base class is also depends on the access modifiers which we used to define those members in base class.

add comment
2

C# | Inheritance
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in C# by which one class is allowed to inherit the features(fields and methods) of another class.

Important terminology:

Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
How to use inheritance

The symbol used for inheritance is :.
Syntax:

class derived-class : base-class
{
// methods and fields
.
.
}

add comment
2

What is Inheritance

Inheritance is one of the three foundational principles of Object-Oriented Programming (OOP) because it allows the creation of hierarchical classifications. Using inheritance you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.

In the language of C#, a class that is inherited is called a base class. The class that does the inheriting is called the derived class. Therefore a derived class is a specialized version of a base class. It inherits all of the variables, methods, properties, and indexers defined by the base class and adds its own unique elements.

add comment

Your Answer