Inheritance in C#

王林
Release: 2024-09-03 15:18:44
Original
1065 people have browsed it

Inheritance in C# is the process of acquiring all the properties of one class into another class. There are two classes referred to as base class and derived class. The base class is also known as a parent class, and the properties or methods of this class we want to inherit to another class.

The derived class is known as the child class that is used to inherit the properties and methods of the base class or parent class. It helps in reusing the same code again, and no need to define the same properties again and again.

Inheritance is one of the powerful concepts or fundamental attributes of object-oriented programming language. It is widely used in all the OOPs based programming language. Its main purpose is to use the same code again. The example of the Basic Structure of Inheritance is given below:

class BaseClass { }
class ChildClass: BaseClass {}
Copy after login

Types of Inheritance in C#

There are different types of Inheritance in C#:

1. Single Level Inheritance

In Single inheritance, there is only one base class and one derived class. It means the child class will inherit the properties of the parent class and use them.

Example:

class BaseClass
{
public void hello()
{
Console.WriteLine("Parent's Hello Method");
}
}
class ChildClass : BaseClass
{
public void World()
{
Console.WriteLine("Child's World Method");
}
}
Copy after login

2. Multilevel Inheritance

In this type of inheritance, there is only one base class, and multiple derived class are available. If a class is created by using another derived class is known as multilevel inheritance.

Example: 

class BaseClass
{
public void hello()
{
Console.WriteLine("Parent's Hello Method");
}
}
class ChildClass : BaseClass
{
public void World()
{
Console.WriteLine("Child's World Method");
}
}
class SecondChildClass : ChildClass
{
public void Hi()
{
}
}
Copy after login

3. Multiple Inheritance

In this type of inheritance, this can be achieved with the help of multiple interfaces, not with a class. In simple words, C# does not support multiple inheritances, but if you want to achieve it, then it can be achieved with the help of interfaces only.

Example:

Interface A {}
Interface B {}
Class C: A, B {}
Copy after login

4. Hierarchical Inheritance

In this type of inheritance, there is one parent class, and the other derived classes inherit the same parent class to achieve this inheritance.

Example:

class BaseClass
{
public void hello()
{
Console.WriteLine("Parent's Hello Method");
}
}
class ChildClass : BaseClass
{
public void World()
{
Console.WriteLine("Child's World Method");
}
}
class SecondChildClass : BaseClass
{
public void Hi()
{
}
}
Copy after login

Advantages of Inheritance in C#

Below are the advantages of Inheritance given.

  • It helps in using the same code again means code reusability.
  • It reduces code redundancy.
  • It helps in reading the code more comfortably.
  • It also reduces the size of the source code and file.
  • It helps in providing the extensibility to code.
  • The code is easy to manage as it divided into classes of the base class and child class.
  • Private members are not accessed in derived class when base class members are inherited by the derived class.
  • It does not support multiple inheritances but can be achieved through interfaces.
  • It helps in achieving the polymorphism that allows an object to represent more than one type.
  • It helps in dividing the large code into small pieces.

Features of Inheritance

Following are the features of Inheritance described.

  • Inheritance can be used to prevent the direct instantiation of the class, and to achieve this, the abstract keyword has been used.
  • The members of the base class can be accessed in derived class except for private key members.
  • The members of the base class can be inherited in the derived class except for the constructor and destructor as well.
  • In C#, the virtual methods of a base class need to use an override keyword in the derived class.
  • In C#, to prevent the inheritance of the class that can be declared with the sealed keyword.
  • If the inherited members need to be hidden with the same name and signature in the derived class, then that method needs to be defined with the new keyword.

Why use Inheritance and How it makes it easy to work?

Inheritance is used when the same code needs to be used in another class. So, instead of writing the same code, again and again, there is a concept of object-oriented programming that is Inheritance that helps in using the same functionality like methods or properties of one class to another class. With the help of semicolon (:), we can inherit the members of the base class to child class or derived class.

It makes it easy to work on because it helps in avoiding the confusion of method calling from which class method is getting called. It makes the code reusability and makes the file lighter in weight with less number of lines of source code. This makes the code less redundant and more flexible to use in different classes. It keeps the structure of the program that helps in reading the code easily.

結論

  • 継承は、すべての OOP ベースの言語および C# で最も広く使用されているオブジェクト指向プログラミングの概念です。これは開発者が多くのことを達成するのに役立ち、コードをよりスムーズで読みやすくします。
  • ポリモーフィズムは、継承のみを使用して達成できるもう 1 つのおっと概念です。これら 2 つの概念を連携させることで、ほとんどの問題が解決されました。
  • 継承では、基底クラスと派生クラスは、基底クラスに変更があるかのように緊密に結合されます。その後、自動的にすべての子クラスが影響を受けます。データ メンバーが適切に使用されずにメモリが割り当てられると、アプリケーションのパフォーマンスに影響を与えるため、継承は非常に慎重に使用する必要があります。
  • アプリケーションのプログラミングや開発では、さまざまなレベルの継承が使用されます。 Web ベースやデスクトップベースのアプリケーションなど、あらゆる種類のアプリケーションに実装できます。継承は多くの柔軟性と機能を提供し、目的を達成するのに役立つため、継承をどこでどのように使用するかは開発者によって異なります。

The above is the detailed content of Inheritance in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!