C# Inheritance Tutorial | A Complete Free Tutorial
C Inheritance Tutorial ACTE

C# Inheritance Tutorial | A Complete Free Tutorial

Last updated on 28th Jan 2022, Blog, Tutorials

About author

Manish Kiran (C# Developer )

Manish Kiran is a C# developer expert and subject specialist who has experience with Git, WPF, WinForms, C#,.Net, SQL, .NET Development, VB, .NET Framework,.NET Core, SVN, and Mercurial. His articles help the learners get insights into the domain.

(5.0) | 18036 Ratings 1923
    • Introduction to C# Inheritance
    • What is Inheritance?
    • How Inheritance is executed in C#?
    • Which kind of inheritance is upheld in C#?
    • Methods of inheritance in C#
    • Abstract and Virtual Methods
    • Types of Inheritance in C#
    • The C# Scopes for Privileged Inheritance
    • Advantage of Inheritance in C#
    • Features of Inheritance
    • Ceritification and Cost
    • Pre-Requisites
    • Conclusion

    Subscribe For Free Demo

    [custom_views_post_title]

      Introduction C# Inheritance:-

      This instructional exercise acquaints you with inheritance in C#. Inheritance is an element of item arranged programming dialects that permits you to characterize a base class that gives explicit usefulness (information and conduct) and to characterizes determined classes that either acquire or abrogate that usefulness.

      What is Inheritance?

      Inheritance is one of the principal credits of article situated programming. It permits you to characterize a youngster class that reuses (acquires), expands, or changes the conduct of a parent class. The class whose individuals are acquired is known as the base class. The class that acquires the individuals from the base class is known as the inferred class.

      C# and .NET help single inheritance as it were. That is, a class can acquire from a solitary class. Be that as it may, inheritance is transitive, which permits you to characterize a inheritance-ordered progression for a bunch of types. All in all, type D can acquire from type C, which acquires from type B, which acquires from the base class type A. Since inheritance is transitive, the individuals from type an area are accessible to type D.

      How Inheritance is executed in C#?

      Inheritance (Derived and Base Class)-

      In C#, it is feasible to acquire fields and strategies starting with one class then onto the next. We bunch the “inheritance idea” into two classifications:

    • Derived Class (kid) – the class that acquires from another class.
    • Base Class (parent) – the class being acquired fro.
    • What is the punctuation to acquire from a class in C#?

      The image utilized for inheritance is Grammar: class inferred class: base-class {//techniques and fields.} Example: In beneath an illustration of inheritance, class GFG is a base class, class Geeks for Geeks is an inferred class which broadens GFG class and class Sudo is a driver class to run program.

      Which kind of inheritance is upheld in C#?

      C# and. NET helps single inheritance as it were. That is, a class can acquire from a solitary class. Nonetheless, inheritance is transitive, which permits you to characterize a inheritance progressive system for a bunch of types,

      Implicit inheritance:

    • Other than any kinds that they might acquire from through single inheritance, various types in the .NET sort framework certainly acquire from Object or a sort got from it. The normal usefulness of Object is accessible to any sort.
    • To see what verifiable inheritance implies, how about we characterize another class, SimpleClass, that is essentially an unfilled class definition:

    • C#Copy
    • public class SimpleClass
    • { }
    • You can then utilize reflection (which allows you to assess a sort’s metadata to get data concerning that sort) to get a rundown of the individuals that have a place with the SimpleClass type.
    • Although you haven’t characterized any individuals in your SimpleClass class, the yield from the model shows that it has nine individuals. One of these individuals is a parameterless (or default) constructor that is naturally provided for the SimpleClass type by the C# compiler.
    • The leftover eight are individuals from Object, the sort from which all classes and points of interaction in the .NET sort framework at last verifiably acquire.

      Methods of inheritance in C#:

      The public ToString method, which changes over a SimpleClass object to its string portrayal, returns the completely qualified sort name. For this situation, the ToString method returns the string “SimpleClass”.

      Three strategies that test for uniformity of two items: the public example Equals(Object) technique, the public static Equals(Object, Object) method, and the public static ReferenceEquals(Object, Object) technique. As a matter of course, these techniques test for reference balance; that is, to be equivalent, two article factors should allude to a similar item.

    • The public GetHashCode technique, which registers a worth that permits an occurrence of the kind to be utilized in hashed assortments.
    • The public GetType technique, which returns a Type object that addresses the SimpleClass type.
    • The safeguarded Finalize method, which is intended to deliver unmanaged assets before an item’s memory is recovered by the city worker.
    • The safeguarded MemberwiseClone technique, which makes a shallow clone of the current item.
    • Due to implied inheritance, you can call any acquired part from a SimpleClass object similarly as though it was a part characterized in the SimpleClass class. For example, the accompanying model calls the SimpleClass.ToString method, which SimpleClass acquires from Object.

      Inheritance – infer types to make more specific conduct:

    • Inheritance, along with embodiment and polymorphism, is one of the three essential qualities of item arranged programming. Inheritance empowers you to make new classes that reuse, broaden, and alter the conduct characterized in different classes. The class whose individuals are acquired is known as the base class, and the class that acquires those individuals is known as the determined class.
    • A determined class can have just one direct base class. Notwithstanding, inheritance is transitive. If ClassC is gotten from ClassB, and ClassB is gotten from ClassA, ClassC acquires the individuals proclaimed in ClassB and ClassA.
    • Theoretically, a determined class is a specialization of the base class. For instance, assuming you have a base class Animal, you may have one determined class that is named Mammal and one more inferred class that is named Reptile. A Mammal is an Animal, and a Reptile is an Animal, however each determined class addresses various specializations of the base class.
    • Interface statements might characterize a default execution for its individuals. These executions are acquired by determining connection points, and by classes that carry out those interfaces. For more data on default interface strategies, see the article on interfaces.
    • At the point when you characterize a class to get from another class, the determined class certainly gains every one of the individuals from the base class, except its constructors and finalizers. The determined class reuses the code in the base class without having to reimplement it. You can add more individuals to the determined class. The determined class broadens the usefulness of the base class.
    • Running the models:

      To make and run the models in this instructional exercise, you utilize the dotnet utility from the order line. Follow these means for every model:

    • 1. Create a catalog to store the model.
    • 2. Enter the dotnet new control center order at an order brief to make a new .NET Core project.
    • 3. Copy and glue the code from the model into your code editorial manager.
    • 4. Enter the dotnet reestablish order from the order line to stack or reestablish the task’s conditions. You don’t need to run dotnet reestablish because it’s run verifiably by all orders that require a reestablish to happen, for example, dotnet new, dotnet construct, dotnet run, dotnet test, dotnet distribute, and dotnet pack. To handicap certain reestablish, utilize the – – no-reestablish choice. The dotnet reestablish order is as yet helpful in specific situations where expressly reestablishing appears to be legit, for example, consistent joining works in Azure DevOps Services or in form frameworks that need to unequivocally control when the reestablish happens.
    • 5. Enter the dotnet run order to gather and execute the model.
    Course Curriculum

    Learn C# Certification Training Course to Build Your Skills

    Weekday / Weekend BatchesSee Batch Details

      Abstract and Virtual Methods:

      Whenever a base class announces a method as virtual, an inferred class can supersede the technique with its execution. Assuming a base class announces a part as theoretical, that method should be superseded in any non-unique class that straightforwardly acquires from that class. If a determined class is itself dynamic, it acquires theoretical individuals without executing them. Conceptual and virtual individuals are the reason for polymorphism, which is the second essential trait of item arranged programming. For more data, see Polymorphism.

      Abstract Base Classes-

      You can pronounce a class as theoretical to forestall direct launch by utilizing the new administrator. A theoretical class can be utilized provided that another class is gotten from it. A theoretical class can contain at least one method mark that themselves is proclaimed as dynamic. These marks determine the boundaries and return esteem however have no execution (method body). A theoretical class doesn’t need to contain dynamic individuals; be that as it may, assuming a class contains a theoretical part, the actual class should be pronounced as conceptual. Determined classes that aren’t dynamic themselves should give the execution to any theoretical strategies from a theoretical base class.

    Types of Inheritance

      Types of Inheritance in C#:

      There are various kinds of Inheritance in C#,

      1. Single Level Inheritance:

      In a Single inheritance, there is just one base class and one determined class. It implies the youngster class will acquire the properties of the parent class and use them.

        • Model:
        • class BaseClass
        • {
        • public void welcome()
        • {
        • Console.WriteLine(“Parent’s Hello Method”);
        • }
        • }
        • class ChildClass : BaseClass
        • {
        • public void World()
        • {
        • Console.WriteLine(“Child’s World Method”);
        • }
        • }

      2. Multilevel Inheritance:

      In this kind of inheritance, there is just one base class, and different determined classes are accessible. Assuming that a class is made by utilizing one more determined class is known as a staggering inheritance.

      Famous Course in this classification

      • Model:
      • class BaseClass
      • {
      • public void welcome()
      • {
      • Console.WriteLine(“Parent’s Hello Method”);
      • }
      • }
      • class ChildClass : BaseClass
      • {
      • public void World()
      • {
      • Console.WriteLine(“Child’s World Method”)
      • }
      • }
      • class SecondChildClass : ChildClass
      • {
      • public void Hi()
      • {
      • }
      • }

    3. Multiple Inheritance:

    In this kind of inheritance, this can be accomplished with the assistance of different connection points, not with a class. In basic words, C# doesn’t uphold different legacies, however, to accomplish it, then, at that point, it very well may be accomplished with the assistance of points of interaction as it were.

        Interface A {}

        Interface B {}

        Class C: A, B {}

    4. Hierarchical Inheritance:

    In this kind of inheritance, there is one parent class, and the other inferred classes acquire a similar parent class to accomplish this inheritanc.

      • Model:
      • class BaseClass
      • {
      • public void welcome()
      • {
      • Console.WriteLine(“Parent’s Hello Method”);
      • }
      • }
      • class ChildClass : BaseClass
      • {
      • public void World()
      • {
      • Console.WriteLine(“Child’s World Method”);
      • }
      • }
      • class SecondChildClass : BaseClass
      • {
      • public void Hi()
      • {
      • }
      • }

      The C# Scopes for Privileged Inheritance:

      You can’t join the different extensions that you can apply to a C# part since I suspect, they wouldn’t seem OK (what’s the extent of a public-private technique, for instance?). In any case, you can join inward with private and safeguarded this way:

        • Model:
        • public class customer base
        • {
        • safeguarded inner void DeleteCustomer() {
        • or on the other hand
        • public class customer base
        • {
        • private safeguarded void DeleteCustomer() {

      In my first model, on account of its inside scope, DeleteCustomer can be utilized by code that launches CustomerBase however provided that that code is in a similar undertaking as to customer base. On account of the safeguarded affirmation, nonetheless, code in different undertakings can get to DeleteCustomer through a class that acquires from a customer base.

      Code in different tasks that launch CustomerBase straightforwardly won’t see DeleteCustomer. Code in a similar task as CustomerBase has an honor denied to code in different activities (the capacity to erase clients, for this situation).

      The degree private safeguarded goes significantly further and is most likely more helpful. This degree keeps DeleteCustomer from being gotten to besides through classes that get from the customer base and provided that those determined classes are in a similar venture as the customer base.

      Code in another venture in a class that acquires from CustomerBase will not have the option to get to DeleteCustomer. This permits you to make determined classes in the very undertaking as CustomerBase that have honors denied to classes in different activities that acquire from the customer base.

    Course Curriculum

    Get JOB Oriented C# Training for Beginners By MNC Experts

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

      Advantage of Inheritance in C#:

      The following are the upsides of Inheritance given.

    • It helps in utilizing a similar code again implies code reusability.
    • It decreases code overt repetitiveness.
    • It helps in perusing the code all the more easily.
    • It additionally lessens the size of the source code and document.
    • It helps in giving the extensibility to code.
    • The code is not difficult to oversee as it is separated into classes of the base class and kid class.
    • Private individuals are not gotten to in inferred class when base class individuals are acquired by the determined class.
    • It doesn’t uphold various legacies yet can be accomplished through interfaces.
    • It helps in accomplishing the polymorphism that permits an item to address more than one kind.
    • It helps in isolating the huge code into little pieces.

      Features of Inheritance:

      Following are the highlights of Inheritance portrayed.

    • Inheritance can be utilized to forestall the immediate launch of the class, and to accomplish this, the theoretical watchword has been utilized.
    • The individuals from the base class can be gotten to in a determined class except for private key individuals
    • .
    • The individuals from the base class can be acquired in the determined class except for the constructor and destructor also.
    • In C#, the virtual techniques for a base class need to involve a supersede catchphrase in the inferred class.
    • In C#, to forestall the inheritance of the class that can be announced with the fixed catchphrase.
    • Assuming the acquired individuals should be concealed with similar names and mark in the determined class, then, at that point, that method should be characterized with the new catchphrase.

      Ceritification and Cost:

      Inheritance diminishes the execution speed because of the expanded time and exertion it takes, the program to go through every one of the degrees of over-burden classes. Inheritance makes the two classes (base and acquired class) get firmly coupled. This implies one can’t be utilized freely by one another.

      Pre-Requisites:

      This instructional exercise expects that you’ve introduced the .NET SDK. Visit the .NET Downloads page to download it. You additionally need a code manager. This instructional exercise utilizes Visual Studio Code, even though you can utilize any code manager of your decision.

    C and C Plus Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

      Conclusion:

    • Inheritance is the most broadly utilized idea of article situated programming in all the OOPs based language thus as in C#. It assists the designer with accomplishing numerous things and makes the code smoother and more intelligible.
    • Polymorphism is the other uh oh idea that can be accomplished with the assistance of Inheritance as it were. The majority of the issues have been settled with these two ideas going connected at the hip.
    • In Inheritance, the base class and inferred class are firmly coupled as though there is any adjustment of the base class; then, at that point, consequently, all the youngster class will get impacted. Inheritance should be utilized cautiously as though the information individuals are not as expected utilized and the memory is dispensed to them, then, at that point, it influences the presentation of the application.
    • There are various degrees of inheritance that are utilized in programming or fostering an application. It very well may be executed in an application like electronic or work area-based applications. It relies upon the designer how and where to involve inheritance as it gives a ton of adaptability, elements, and assists with accomplishing the things.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free