Inheritance in c#

 Introduction: 

Inheritance is a concept of object oriented programming, in which a sub class or a child class inherits the super class or the parent class and it then the access the variables or the methods defined inside that particular class. In this process a child class can have access to the public or protected variables and methods of the parent class or the base class.

Step 1:

In this example we will learn how does Inheritance takes place in c#, so first up create new a project in visual studio 2017, We will name it as InheritanceBasics and click OK.


Step 2:  We will create a base class and we will define a few data members along with methods inside the base class.

  
Step 3: We will create a child class or a derived class, so that it will inherit the base or the parent class which we named as Base.
So we are creating another method inside the child class and we will get the Area of a circle from that method, we have named our child class as Derived, this class is accessing the protected data members of the parent class to calculate the Area of a circle.



Step 4: Here in the Main function we will create an object for the derived class to access its corresponding methods.


 So here we created an object for the child class Derived and we called the two methods of the base class first to set the width and height of the circle and finally it calls the function AreaofCircle(), which returns the area of a circle.

Here is how the entire code looks like.


 Press crtl+f5 to run the program and see the output window.






Talha Mahmood

I have been developing Applications in Microsoft .NET technologies since 2015, and having more then 5 years of experience in .NET Technology and SQL database, also having hands in tools like C# Windows Forms, WPF, WCF, Asp.Net Web forms, Asp.Net Core, SSRS, LINQ, Entity Framework, Crystal Reports, SQL server, MySQL, MongoDB, Document DB, JQuery, JavaScript, Oracle, Web API.

Post a Comment

Previous Post Next Post